List Matching Lines and Delete Matching Lines in Emacs

Advertise Here

, 2010-05-03, 2011-07-28

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 buffer 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-lines
sort-numeric-fields
reverse-region

Hotkeys & Aliases

If you use them often, you can give them a keyboard shortcut such as F8 or 【Alt+1】. See: Emacs: How to Define Keyboard Shortcuts.

My hot key spots are already filled, so i use aliases.

list-matching-lines       │ lml
delete-matching-lines     │ dml
delete-non-matching-lines │ dnml
sort-lines                │ sl
sort-numeric-fields       │ snf
reverse-region            │ rr
See: Defining Alias to Increase Productivity.

Delete Starts at Cursor Position or Text Selection

delete-matching-lines and delete-non-matching-lines starts at the line your cursor is on. So, if you want deletion to happen for the whole file, you need to move to the beginning of file first.

Also, if you have a text selection, the deletion happens in the text selection only.

Escape Regex Chars

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 charsliteral
[\[
]\]
\\\
+\+
*\8
?\?
.\.

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 the cases to be case sensitive (that is, literally what you gave), then you need to set the variable “search-upper-case” to “nil” (nil means false).

You can see the current value of a variable by the command describe-variable.

You can change a variable's value by the command set-variable.

List Lines in ALL Buffer

Use multi-occur if you want to list lines in ALL buffers.

Elisp Exercise

Here are some ideas of commands related to “list-matching-line” that would make emacs even more useful. They are good elisp exercises. Each of the following will take 5 to 30 minutes to write.

list-non-matching-lines

Write a list-non-matching-lines.

This would be convenient. I needed it often.

list-matching-lines-no-regex

Write a list-matching-lines-no-regex.

Often, you want to list lines by word or phrase, not regex. If your search text often contains regex chars, it'll take you extra ~3 seconds to escape them. Write a version of list-matching-lines that does not use regex. (hint: write a wrapper that calls list-matching-lines, using regexp-quote to quote the input.)

Write a list-matching-lines2 that takes current word as input.

When using list-matching-lines, it would be nice if the current word under cursor will be the default search text. Or, if there's a text selection, use the text selection as default search phrase. This will save you 5 or more keystrokes or few seconds to mark and copy and paste. (See a solution here: Emacs Lisp list-matching-lines)

split-buffer-by-matching-lines

Often, i need to see what lines contain a certain word, then delete those lines. Effectively, i call list-matching-lines, then call delete-matching-lines with the same input. When i do this many times, the typing to call these commands and input is repetitious (even using the single key press of up arrow because of command history feature). It'd be nice, to have something like split-buffer-by-matching-lines, so that, it delete matching lines and show the deleted lines in a different buffer.

blog comments powered by Disqus