8

Is it possible to delete specific columns of text (i.e., particular column numbers) in Emacs/XEmacs using only the keyboard? I know that it is possible to select a rectangle of text using the mouse (or the arrow keys), but for a 1 million line text file, this can be quite time consuming.

Thanks!

Andrew
  • 1,499
  • 9
  • 25
  • 37

2 Answers2

10

You can delete a rectangle with delete-rectangle (C-x r d) or kill-rectangle (C-x r k). Either one will kill the rectangle with corners defined by the point and the mark. kill-rectangle will also save it for yanking with yank-rectangle.

ataylor
  • 64,891
  • 24
  • 161
  • 189
  • 1
    Thanks. But how do I make a selection rectangle quickly (without using the mouse), for a one million line text file? I have tried holding down shift while holding Page Up or Page Down to quickly scroll through and select text, but this is still relatively slow. – Andrew May 07 '12 at 17:24
  • 1
    I suggest writing a quick SED script for jobs that large. – kmarsh May 07 '12 at 17:27
  • 2
    If you want to delete the column for every line, first set the mark at the beginning of the column on the first line, then hit `end-of-buffer` (`M->`), move to the end of the column on the last line, and run `delete-rectangle`. – ataylor May 07 '12 at 17:28
  • sed might indeed be a better choice, or a small elisp program run with `emacs --batch --script`. – ataylor May 07 '12 at 17:31
  • Thanks. Even when I hold shift, `M->` brings me to the end of the buffer but does not select--the selection is dropped, at least in XEmacs on Windows. I guess I will need to write a script, but I have never done this. – Andrew May 07 '12 at 17:44
  • 3
    You don't need to hold shift. Start by setting the mark with `C-space`, then `M->`. The region should now be active. Even though it will be highlighted as lines, rectangle commands will still operate on a rectangle. – ataylor May 07 '12 at 17:58
1

If you have sed or awk on your system, you can conveniently use C-u M-|.

From the documentation:

M-| runs the command shell-command-on-region [...]

Execute string COMMAND in inferior shell with region as input. Normally display output (if any) in temp buffer `Shell Command Output'; Prefix arg means replace the region with it. Return the exit code of COMMAND. [...]

Note the bit about the prefix arg (C-u).

Tip: C-x h will set the region to your whole buffer.

daveloyall
  • 2,140
  • 21
  • 23