M-x all-things-emacs

Quick Tip: Add occur to isearch

February 27th, 2007 by Ryan McGeary · 10 Comments

Zenspider gives us an excellent tip for extending incremental search. Add this to your emacs initialization:

1
2
3
4
5
6
(define-key isearch-mode-map (kbd "C-o")
  (lambda ()
    (interactive)
    (let ((case-fold-search isearch-case-fold-search))
      (occur (if isearch-regexp isearch-string
               (regexp-quote isearch-string))))))

This adds an extra keybinding to interactive search (C-s) that runs occur on the current search string/regexp, immediately showing all hits in the entire buffer. I use it all the time now.

Seeing as how I run incremental searches subconsciously before even thinking about running occur, I can see myself using this shortcut constantly. I tried it on the emacs tutorial (C-h t):

C-s foo C-o
Incremental Search with Occur

Tags: elisp · isearch · quick · tips

10 responses so far ↓

  • 1 tom // Feb 27, 2007 at 4:35 pm

    seems to be turned on by default in debian? I have this behaviour without turning it on…

  • 2 sean // Feb 27, 2007 at 5:39 pm

    For this tip’s likely original (uncredited) source — http://www.emacswiki.org/cgi-bin/wiki/OccurFromIsearch — and many other tips, check out the Emacs Wiki at http://www.emacswiki.org/ .

  • 3 Ryan McGeary // Feb 27, 2007 at 7:39 pm

    Sean, Thanks for the (likely) original source. I tried a quick google search to see if there were any other prior credits to be given, but I obviously didn’t spend enough time on that.

  • 4 sean // Feb 27, 2007 at 10:30 pm

    Nah, I probably would not have found it if I hadn’t picked it up off the wiki a long time ago, stuck it in my .emacs, and used it more-or-less constantly ever since.

  • 5 piyo // Feb 28, 2007 at 9:12 am

    That is a great tip that is new to me. Thanks!

    The following maybe well-known tips (they are definately standard features on Emacs 22.0.90 “pretest”):

    I also like pressing M-% to drop into query-replace. (I think creating this feature for Emacs 19 was a topic in Bob Glickstein’s Writing Gnu Emacs Extensions.)

    I also like pressing M-e to edit the current isearch string non-incrementally.

    I rebind C-s from isearch-forward to isearch-forward-regexp. (C-r is similar.) If I really need a non-regexp isearch, I either press C-u before C-s, or press M-r during the isearch.

    The Emacs manual has some good information about these special bindings: (info “(emacs)Special Isearch”). I also recommend looking at (describe-function ‘isearch-mode) since it is not possible to get the mode map using the usual Ctrl-h key.

    What other modes would be interesting to “drop into”? Regexp-builder to isearch and/or vice versa?

  • 6 user // Mar 3, 2007 at 7:54 pm

    Have you seen this? It’s very similar only more sophisticated:

    http://www.emacswiki.org/cgi-bin/emacs-en/Icicles_-_Search_Enhancements

  • 7 sean // Mar 5, 2007 at 7:48 am

    Yes, I’ve seen the Icicles commands. If you can get used to them, more power to you.

  • 8 Newbie Tip: isearch Word Yank | M-x all-things-emacs // Mar 26, 2007 at 7:00 am

    [...] For a full listing of available isearch modifier keys, review the help for isearch (C-h f isearch-forward). It is also possible to add your own functions to the isearch-mode-map. Last month, we referenced a way to add occur to isearch. [...]

  • 9 Alex // Apr 25, 2007 at 8:35 am

    Thank You

  • 10 sandrar // Sep 10, 2009 at 10:12 pm

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.