Deleting specific columns of text in Emacs/XEmacs using the keyboard
Asked Answered
P

2

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!

Psychoactive answered 7/5, 2012 at 17:17 Comment(0)
H
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.

Highcolored answered 7/5, 2012 at 17:20 Comment(6)
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.Psychoactive
I suggest writing a quick SED script for jobs that large.Morey
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.Highcolored
sed might indeed be a better choice, or a small elisp program run with emacs --batch --script.Highcolored
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.Psychoactive
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.Highcolored
S
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.

Smalley answered 7/4, 2015 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.