The OCCUR Command in Emacs

Intro

Note: the occur command has a synonym: list-matching-lines

M-s o

What it does: Displays lines that match a string as a list in an separate buffer called the occur buffer

It is like doing a grep search but on open buffer(s), rather than on file(s)

Why it is useful

You can see an overview of all hits

You do not need to iterate through hits one match at a time

You can edit all hits in one place

Particulars

M-p M-n reuse strings from previous incremental searches

Context lines can be specified using a prefix argument.

Sometimes context lines are helpful, other times they get in the way.

Note: One can switch to an occur search when isearch is active

Note: Overlapping matches are not identified as a match

Compare searching for:

ababa

with:

abcabcd

Once the occur buffer is open

Go into the occur buffer

Note that now occur-mode is the major mode

n and p move to next or previous matches

Hits are matched across the two buffers

Accepts universal arguments

Digit keys can be used without prefix arguments. So 2 n moves to the second next match

SPC and DEL scroll the Occur buffer up and down

C-o displays the match in another window but does not switch to the other window

o displays the match in other window and switches to other window

M-g M-n allows you to navigate the hits from the non-occur buffer

q quits the occur buffer (you have to be in it)

Editing in the occur buffer

e makes the buffer writable and enters Occur Edit Mode

C-c C-c saves the changes and/or exits Occur Edit Mode

One can use macros in this mode and search and replace

Settings

(setq list-matching-lines-default-context-lines 2) sets default value of context lines

Return to Home