Thursday, August 2, 2007

Emacs Example Tutorial: kill a block of text

When trying to learn to use Emacs, I had a very hard time finding out how to kill a multi-line block of text in Emacs. When I say "block" I mean a square or rectangle that does not include anything outside the marked area, in other words, not including the rest of the lines - whatever portion I didn't mark. For example, the equivalent of "Block Mode Editing" like in Kate, or UltraEdit.

If you go search on google for this kind of block "cut", you'll normally find documentation telling you to use C-w (note to newbies: in emacs documentation when you see "C-", that means the Control key, and when you see "M-", that means the Meta key, which is the "Alt" key for most PC users. So C-w is Control-w). This kills all text on every line, including the end of the line, within the whole region, from the Mark to the Point. When I am trying to trim out a block of text from multiple lines (such as an unusually large comment block, or a portion of a column from a tabulated text file), I don't want the whole line killed, just the letters and other characters inside the square block.

I finally found that this is called killing the "region-rectangle", and the command is:

    C-x r k

For those that don't know: you "mark" a region in Emacs 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 the cursor (I always use the arrow keys, but I guess there's 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.


Anonymous said...

Thank you, thank you, thank you!
Thank you lots for this post!! It saved me a lot of even more time spent in the Emacs Guide. I knew there was a command for this, but I absolutely did not find it elsewhere.


Joseph C. said...

Thanks! I was looking for this, too - I had used it so long ago, I had forgotten about block-kill and block-yank (C-x r y)

steveb said...

Thanks. Nice documentation. One question. When I mark with Ctrl+Space and use the arrows to highlight a section of text I can only select full lines not a rectangle within multiple lines. For example I can't select the first word on all the lines & mark it. I'm guessing my marking is set to mark only lines but I don't know where to change that. Thoughts?

JW said...

@steveb: You must be using the X-windows GUI for emacs. In spite of the way it looks (highlighting the whole line) the kill region is always a "box" between where ever you hit Control+Space and your current cursor position.

In other words, ignore the fact that it is highlighting the whole line and try marking and killing the region you want - it will work.


steveb said...

Ah, got it thanks.

Anonymous said...

was always getting confused with block mode editing as xemacs showed the whole line .

This article cleared my doubts.