emacs: text manipulation
Text manialations is one aspect Emacs is especially good at, and it has a variety of tools to help you. Massaging text files for further processing or extracting pertinent information from log files are both common things to do in Emacs.
Editable Occur
I introduced M-x occur in Occur(M-s o in helm): Print lines matching an expression as a way of collating all lines that match a certain pattern.
You can typing e begin to edit, and after you finish, C-c C-c to commit the changes to their original lines, it is especially great for keyboard macros and search & replace.
Deleting Duplicates
By default, M-x delete-duplicate-lines deletes the first dupli- cate line it encounters, starting from the top. With a single universal argument, it starts from the bottom and therefore deletes the last.
Univeral Argument | Description |
---|---|
Without | Delete first duplicate line |
C-u | Delete last duplicate line |
C-u C-u | Delte only adjacent duplicates |
C-u C-u C-u | Does not delete adjacent blank lines |
Flushing and Keeping Lines
Sometimes you want to filter lines in a region by a pattern; whether that is to flush lines that match a pattern, or keep the ones that do.
Both commands act on the active region so it is common – if you want to do this on a whole buffer - to call C-x h to select the entire buffer first.
Command | Description |
---|---|
M-x flush-lines | Flushes (deletes) all lines in a region that match a pattern |
M-x keep-lines | Keeps all lines in a region that match a pattern and removes all non-matches |
Keeping lines that match a pattern is useful for large log files then you want to.
Joining and Splitting Lines
Unlike the kill commands that act on lines
(C-M-<backspace>
and C-k), these commands won’t alter
your kill ring. They are also more specialized, as they insert or remove
lines with- out moving your point.
Key Binding | Description |
---|---|
C-o | Inserts a blank line after point |
C-x C-o | Deletes all blank lines after point |
C-M-o | Splits a line after point, keeping the indentation |
M-ˆ | Joins the line the point is on with the one above |
Whitespace Commands
Managing whitespace is an issue that recurs often when you yank text from elsewhere or if you work with languages where whitespace is significant.
Command | Description |
---|---|
M-SPC | Deletes all but I space or tab to the left and right of the point |
M-x cycle-spacing | As above but cycles through all but one, all, and undo |
M-\ | Deletes all spaces and tabs around the point |
M-SPC is useful as it trims all whitespace, to the left or right of the point, to a single whitespace character. ving none. M-x cycle-spacing cycles between leaving one, leaving none, and restoring the original spacing.