isearch is probably one of the most widely known Emacs commands. Every Emacs user knows that they can run it using C-s (to search forward) and C-r to search backwards. Everyone also knows they can keep pressing C-s and C-r to go over the list of matches in the current buffer. Even at this point that’s a very useful command. But that doesn’t even scratch the surface of what isearch can do!
After you’ve started isearch you can actually do a lot more than pressing C-s and C-r:
- Type
DELto cancel last input item from end of search string. - Type
RETto exit, leaving point at location found. - Type
LFD(C-j) to match end of line. - Type
M-s M-<to go to the first match,M-s M->to go to the last match. (super handy) - Type
C-wto yank next word or character in buffer onto the end of the search string, and search for it. (very handy) - Type
C-M-dto delete character from end of search string. - Type
C-M-yto yank char from buffer onto end of search string and search for it. - Type
C-M-zto yank from point until the next instance of a specified character onto end of search string and search for it. - Type
M-s C-eto yank rest of line onto end of search string and search for it. - Type
C-yto yank the last string of killed text. - Type
M-yto replace string just yanked into search prompt with string killed before it. - Type
C-qto quote control character to search for it. - Type
C-x 8 RETto add a character to search by Unicode name, with completion.
C-g while searching or when search has failed cancels input back to what has been found successfully. C-g when search is successful aborts and moves point to starting point.
You can also toggle some settings when isearch is active:
- Type
M-s cto toggle search case-sensitivity. - Type
M-s ito toggle search in invisible text. - Type
M-s rto toggle regular-expression mode. - Type
M-s wto toggle word mode. - Type
M-s _to toggle symbol mode. - Type
M-s 'to toggle character folding. - Type
M-s SPCto toggle whitespace matching.
In incremental searches, a space or spaces normally matches any whitespace defined by the variable search-whitespace-regexp; see also the variables isearch-lax-whitespace and isearch-regexp-lax-whitespace.
Type M-s e to edit the search string in the minibuffer. That one is super useful! Also supported is a search ring of the previous 16 search strings:
- Type
M-nto search for the next item in the search ring. - Type
M-pto search for the previous item in the search ring. - Type
C-M-ito complete the search string using the search ring.
Last, but not least - you can directly search for the symbol/thing at point:
- Type
M-s .to search for the symbol at point. (useful in the context of programming languages) - Type
M-s M-.to search for the thing (e.g. word or symbol) at point.
One of the most useful parts of that is the fact that a region is a thing. So you can mark a region (e.g. with expand-region or mark-*) and M-s M-. to immediately search for other instances of that text. Powerful stuff!
Tip: You don’t really have to remember all those keybindings - just remember you can press C-h b to show them. (after you’ve started isearch) Most of the above text is coming straight from the docstring of isearch.
It’s funny that I’ve been using Emacs for almost 20 years, I use isearch numerous times every day and I still often forget about much of its functionality. There’s more to isearch, though. Did you know it’s widely customizable as well? If you check its options with M-x customize-group isearch you’ll see there are over 30 (!!!) options there! Admittedly, I never used any of them, but you’ve got quite a lot of opportunities to tweak the behavior of isearch if you want to. Here’s an example of a customization some of you might find useful:
;; When isearching, enable ``M-<``, ``M->``, ``C-v`` and ``M-v`` to skip between matches
;; in an intuitive fashion. Note that the ``cua-selection-mode`` bindings
;; for ``C-v`` and ``M-v`` bindings are not supported. (``setq isearch-allow-motion t isearch-motion-changes-direction t``)
I hope you learned something useful today! Keep searching (the Emacs docs)!`___`