This page is a tutorial on working with lines in emacs.
Emacs has a very useful command list-matching-lines. Try it. Call it 【Alt+x】, then, give a word. Emacs will list all lines of current file containing that word.
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 line processing commands for the current buffer that i use often:
list-matching-lines (this is a alias to occur)delete-matching-lines (this is a alias to flush-lines)delete-non-matching-lines (this is a alias to keep-lines)sort-linessort-numeric-fieldsreverse-regionhighlight-lines-matching-regexpIf you use them often, you can give them a key such as F8, or alias such as “sl” for sort-lines.
See: How to Define Keyboard Shortcuts ◇ Defining Alias to Increase Productivity.
delete-matching-lines and delete-non-matching-lines starts at the line your cursor is on.
Also, if you have a text selection, the deletion happens in the text selection only.
All these commands uses 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 some commonly used regex characters that you'll need to replace:
| regex chars | literal |
|---|---|
[ | \[ |
] | \] |
\ | \\ |
+ | \+ |
* | \* |
? | \? |
. | \. |
See also: common patterns in emacs regex.
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 the cases to be case sensitive (what you typed exactly), call toggle-case-fold-search.
Use multi-occur if you want to list lines in ALL buffers.