Standard Emacs command
‘occur’ lists all lines of the current buffer that match a regexp that you give it. The matching lines are listed in buffer
‘*Occur*’, and you can click them there to navigate to the corresponding lines in the original buffer. That is, buffer
‘*Occur*’ acts as a hypertext index to your buffer.
This is so useful that you might want to use a keybinding in your init file for it. For example:
(global-set-key (kbd "C-c o") 'occur)
Starting with Emacs 24, you can use
‘C-o’ within isearch to run
In Emacs 23, you can also use
‘M-s o’ within isearch to run
Starting with Emacs 22, you can also use `
C-x `’ (
‘M-g p’ (
‘previous-error’) to navigate to the next or previous occurrence. You can also turn on
‘next-error-follow-minor-mode’ in the Occur buffer, which binds
I bind next-error and previous-error to the keypad / and * keys, since I don’t use them otherwise. It’s handy and much easier on the fingers than chording Control+X. – TedZ?
Library replace+.el provides some extensions for the standard Emacs
*Occur*buffer. The effect is similar to that of highlighting with the Google toolbar.
*Occur*buffer, line numbers of visited lines are highlighted, so you can see that you have already visited them.
*Occur*buffer, you can click anywhere on a found line: the entire line is mouse-face active.
*Occur*buffer is shrink-wrapped to fit the matching lines.
Here is a screenshot of the
‘*Occur*’ buffer and a visited occurrence in the target buffer, showing match-string highlighting, visited-line number highlighting, and full-line mouseover highlighting (underline):
‘icicle-occur’ in Icicles offers functionality similar to
‘occur’, but with some additional features, including the following:
‘*Completions*’is used (with Completion) for the “occur” matches for your input. The matching occurrences are updated on the fly as you edit your input (which can be a regexp) – incremental occur.
‘icicle-occur’is a multi-occur (like Emacs 22 commands
‘multi-occur-in-matching-buffers’). All Icicles search commands can be used to search multiple buffers simultaneously. You can choose the buffers individually, using completion, or pick all that match a regexp.
‘icicle-occur’on only part of it.
Here is a screenshot taken when a user is cycling among occurrences that match the regexp `
regexp.*match’. It shows:
‘*Completions*’playing a role similar to buffer
‘*Occur*’in standard Emacs
‘occur’. The occurrence highlighted light blue is the next one (for cycling).
You can cycle among the search hits, navigating to each in turn, with
‘C-mouse-2’ in buffer
‘*Completions*’ to go directly to particular hits. To choose a hit as final destination and end search navigating, use
‘C-’). There are many more options – for more info, use `
C-?’ while navigating.
If your MiniBuffer input matches a unique search hit, then you can also get to the hit using completion (
‘S-TAB’), followed by
‘RET’. If there are multiple identical hits that match your input, then cycle to the one you want or use
‘mouse-2’ to choose it.
‘C-M-mouse-2’ (or any of the other
‘C-M-’ keys) will tell you the buffer and location of any hit, in the minibuffer.
The big brother of
‘icicle-search’. It generalizes the functionality of
‘occur’. The units tested for matches are not necessarily lines, as they are for
‘icicle-occur’; they can be any textual unit: paragraph, sequence of words, page – anything. A regexp you provide defines this unit and thus the set of entities to be matched (matching is then done with another regexp). Command
‘icicle-occur’ is a special case of
‘icicle-search’ where the initial regexp is `
.*’, which establishes lines as the text units to search for matches.
‘icicle-search’ are bound to `
C-c '’ and `
C-c `’, respectively, when Icicle mode is turned on.
‘C-M-next’, and so on
Anything is a candidate selection framework.
‘M-x anything-occur’, narrow the matched lines by typing some patterns(multiple patterns are space-delimited string), select with up/down/pgup/pgdown/C-p/C-n/C-v/M-v, choose with enter,
With C-z selected line is displayed without quitting anything session.
(defun occurs (&optional arg) "Switch to *Occur* buffer, or run `occur'. Without a prefix argument, switch to the buffer. With a universal prefix argument, run occur again. With a numeric prefix argument, run occur with NLINES set to that number." (interactive "P") (if (and (not arg) (get-buffer "*Occur*")) (switch-to-buffer "*Occur*") (occur (read-from-minibuffer "Regexp: ") (if (listp arg) 0 arg))))
Since [who?] didn’t like the original behaviour of
‘occur’, [who?] redefined one of the functions:
(defun occur-mode-goto-occurrence () "Go to the occurrence the current line describes. This function redefined by alex! Instead of using `pop-to-buffer' it now uses `switch-to-buffer'." (interactive) (let ((pos (occur-mode-find-occurrence))) (switch-to-buffer occur-buffer) (goto-char (marker-position pos))))
A numeric argument n specifies that n lines of context are to be displayed before and after each matching line.
e.g. to show 5 line before and after each matching line:
C-U 5 M-x occur regexp-to-search
This allows you to call
‘occur’ on an occur buffer. Assume you have a buffer A. Run
‘occur’ gives you a buffer B. Run
‘occur’ again. This will rename buffer B and give a you buffer C. If you select a line in buffer C, that will pop you to the match in buffer B. Select the line again and now you are back in buffer A. As you can see, this is not optimal and it seems that Emacs 21 will have a better way to deal with this.
(defadvice occur (before occur-before) "Allow mutliple calls to occur..." (if (string-match "\*Occur\*" (buffer-name)) (rename-buffer (buffer-name) t))) (ad-activate 'occur)
This gets rid of the line numbers and the header line, so that the result is more like the output from
‘grep’. You might want to bind this to C-c C-x.
(defun occur-mode-clean-buffer () "Removes all commentary from the *Occur* buffer, leaving the unadorned lines." (interactive) (if (get-buffer "*Occur*") (save-excursion (set-buffer (get-buffer "*Occur*")) (goto-char (point-min)) (toggle-read-only 0) (if (looking-at "^[0-9]+ lines matching \"") (kill-line 1)) (while (re-search-forward "^[ \t]*[0-9]+:" (point-max) t) (replace-match "") (forward-line 1))) (message "There is no buffer named \"*Occur*\".")))
Starting with emacs version 22, there is a command named
multi-occur, which searches in all of Emacs’s buffers (that are visiting files), not just the current one.
‘icicle-occur’, which also lets you search all buffers. More flexibly, you can choose any number of buffers to search. Also, you can change regexp dynamically. (See above, IciclesOccur.)
Is it possible to force multi-occur to not create more than one open window with the matching buffer? For instance I would like to to keep the Occur buffer on the left and a single buffer on the right, use next-error or “Auto Occurrence Display” and not have have occur create a new window for each each new buffer.
Put this file into load-path’ed directory, and byte compile it if desired. And put the following expression into your ~/.emacs.
Run ‘occur’. occur buffer will be displayed and the matched line will be showed to other window.
See also Lisp:replace+.el (Replace+, above), for another way to highlight occur matches.
The following deletes the context around the match on the same line.
(defun occurrences (regexp &rest ignore) "Show all matches for REGEXP in an `occur' buffer." ;; keep text covered by occur-prefix and match text-properties (interactive (occur-read-primary-args)) (occur regexp) (with-current-buffer (get-buffer "*Occur*") (let ((inhibit-read-only t) delete-from pos) (save-excursion (while (setq pos (next-property-change (point))) (goto-char pos) (if (not (or (get-text-property (point) 'occur-prefix) (get-text-property (point) 'occur-match))) (if delete-from (delete-region delete-from (point)) (setq delete-from (point))) (when delete-from (delete-region delete-from (point)) (if (get-text-property (point) 'occur-prefix) (insert "\n") (insert " "))) (setq delete-from nil)))))))
list-matching-lines does almost the same as occur. There’s also delete-matching-lines.
There is a minor mode for Emacs acting like occur but w/o creating a new window. It just hides all the text excepting lines containing matches. To use it, add the following to your .emacs file:
(require 'loccur) ;; defines shortcut for loccur of the current word (define-key global-map [(control o)] 'loccur-current) ;; defines shortcut for the interactive loccur command (define-key global-map [(control meta o)] 'loccur) ;; defines shortcut for the loccur of the previously found word (define-key global-map [(control shift o)] 'loccur-previous-match)
Now you can point the cursor to the word and press “Ctrl+o” to hide all lines except those containing this word. Moving cursor to the required line and pressing “Ctrl+o” again will shows all the text. The good thing about this mode is what you can navigate through the buffer easily. “Ctrl+Shift+o” will repeat last search.
‘next-error’ I like to keep the
‘*Occur*’ window visible just like when I open the matches by pressing
‘RET’ on them in the
‘*Occur*’ window. If I use
‘next-error’ from an other window then it reuses the
‘*Occur*’ window and displays the match in it (Emacs 23). The following advice selects the
‘*Occur*’ window before calling
‘occur-next-error’ thereby keeping it displaying the matches and showing the match in the other window:
(defadvice occur-next-error (before my-occur-next-error activate) (let ((win (get-buffer-window (current-buffer)))) (if win (select-window win))))
I don’t have a problem in this regard, because I use a dedicated window for
(setq special-display-regexps '("[ ]?[*][^*]+[*]"))
But this solution opens an additional frame for occur by default which I don’t want, so it would need further configuration to prevent that. The solution above doesn’t have this problem, because it operates in the current frame. – anon