Emacs: List/Delete/Highlight Matching Lines, Sort/Reverse Lines
This page is a tutorial on working with lines in emacs.
Emacs has a very useful command
list-matching-lines. Try it. Emacs will list all lines of current file containing that word.
list-matching-lines 【Alt+s o】. It'll list all lines in the current file that matches a given text. Here's a sample output:
Clicking the line will jump to the match location in the file.
If you want to match with word boundary, use
\b, like this
\bcat\b. This will find “cat” but not “cats”. [see Emacs: Regex Tutorial]
You can click on any matched line in the output, then emacs will put cursor at the position of the occurrence in your file.
There are also several other userful line processing commands:
delete-duplicate-lines(Emacs 24.4) delete duplicated lines in text selection.
If you use them often, you can give them a key such as F8, or alias such as “sl” for
Delete Starts at Cursor Position, or Text Selection
delete-non-matching-lines starts at the line your cursor is on.
If you have a text selection, the deletion happens in the text selection only.
Escape Regex Chars
All these commands use regex to search. So, if you simply want to search plain words or phrases, and if your phrase contains any of regex characters, you need to escape them. Here's some commonly used regex characters that you'll need to place a backslash before it:
[ ] \ + * ? .
See also: common patterns in emacs regex.
Letter Case Sensitivity
In all these commands, if your search word contains upper case letters, then the search is automatically case sensitive. Otherwise, it is not case sensitive.
If you want case sensitive, Alt+x
toggle-case-fold-search. Remember to toggle it back. Best to give a key.
List Lines in ALL Buffer
multi-occur if you want to list lines in ALL buffers.
Patreon me $5. Ask me question on patreon