Wednesday, October 15, 2008

Emacs Example Tutorial: insert a block of text

As I explained in my previous Emacs tutorial on killing a block of text, it is sometimes hard to find easy to understand documentation on how to use emacs.
One of the first things I wanted to learn how to do in Emacs was block editing. By "block editing" I mean select a vertical area of text (one or more columns across multiple rows making a square or rectangle - you might call it zero or more since you don't actually have to select any characters, only the spaces between them) and do things to it - you might call it column editing or column mode or "Block Mode Editing" like in Kate, or UltraEdit.

Inserting a column of characters in emacs (without using a mouse) is really easy: you start by highlighting or selecting an area - when you do this you are said to "mark" the region.

You mark a region by positioning your cursor in one corner of the area you want to mark and hitting Control-Spacebar. You will see Emacs respond with the message "Mark set". Then navigate your cursor (I always use the arrow keys, but there is probably other ways to do it) to the opposite corner of the region you want to mark. The "region" is now marked as a rectangle between wherever you started (when you hit Ctrl-Space) and wherever your cursor is now sitting. Note that you don't do anything special to mark the end of the region -- the end is where ever your cursor is positioned.

Emacs calls the space you've now selected the "region-rectangle". Once it's highlighted, you can insert characters into the region by typing:

C-x r t

(That means press x while holding down the control key, release both keys, press and release the r key, press and release the t key). Emacs will respond with the message "String rectangle": it is asking you what text you wish to insert. Type what ever characters (letters) you want to "paste" into the region, and hit enter. It will automatically fill in that same text on every line that you've selected in your region.

I find this to be a great way to comment out areas of code with # characters. Emacs also has a "comment region" command which sometimes works, but occasionally emacs is too smart for it's own good and doesn't know what comment character to use (in which case it will usually ask you which to use with the question "No comment syntax is defined. Use:"), or worse it will use the wrong one.

You can use the comment region function by selecting a region in the same manner descried above, then type:

M-x comment-region

(That means press x while holing down the ALT key, release both keys, type "comment-region" - without the quotes of course - and hit enter).

I find C-x r t easier and faster to use than "M-x comment-region" in most cases anyway. It's fewer letters to type, even when you consider tab completion.


plh said...

I use M-; (bound to comment-dwim) for all my commenting needs. It comments/uncomments regions etc. smartly and according to the value of comment-style (which is customizable). If you know of a mode which sets the commenting characters wrong (or not at all), you should probably send a bug report.

Also, I think Emacs is the best documented application I've ever come across: the texinfo manuals are pretty comprehensive and well-written and easily accessible from the menu or with C-h. Every command, variable and key sequence is (self-) documented, there are refcards galore in the Emacs /etc directory and there are numerous ways to find help from within Emacs itself. For example, if I hadn't known how to kill a rectangle region, I would've tried C-h a rectangle RET or C-h r m rectangle RET before resorting to the emacswiki, Google or #emacs.

JW said...

Hi plh,

Thanks, I didn't know about M-;.

While I agree that emacs is extensively documented, I would not say that the documentation is easy to use, especially if you are not familiar with all the special jargon.
- JW

Dan Lewis said...

I stumbled across rectangle kill while trying to do register kills and yanks. I find both very useful now.

I agree that you have to know what you're looking for before you can use all the power of the documentation. Skimming the manual is a good idea. I like M-x apropos slightly better than C-h a because it doesn't just search interactive commands, but includes functions and variables, etc.

Anonymous said...

In C/C++ Mode, comment-region is bound to C-c C-c keys, so you can mark the region and use these keys to comment out the region.

Anonymous said...


Anonymous said...

Top web site, I hadn't noticed previously during my searches!
Keep up the good work!

Melissa said...

Thanks, your solution is what I needed.