5

I want to be able to search for some text and navigate quickly between the occurrences. What is a convenient way to search for some text, view all the occurrences, and jump quickly to the next/previous occurrence?

2
  • 3
    After @Gilles's edit, this is a good and clear question. There are various fancy packages that can do it, they should be mentioned in the answers. Emacs built-in help system won't tell user about them. Commented Aug 21, 2015 at 11:12
  • @Drew hover your mouse over the upvote arrow, what does it say? You opinionated stupid asshole.
    – towry
    Commented Oct 26, 2018 at 8:15

3 Answers 3

22

The manual is your friend!

The basic search capability is incremental search. Press C-s and enter the string to search (C-r to search backwards). Press C-s/C-r again to go to the next/previous occurrence. Any command other than typing text, backspace and a few others exits search mode; in particular, press RET to continue editing where the occurrence was found or C-g to go back to the starting point.

Press C-s C-s to repeat the previous search (C-r C-r to repeat it backwards). After entering incrementatl search mode with C-s or C-r, press M-p/M-n to browse the search history (previous/next). Press C-s C-w to search for the word under the cursor (and other similar commands).

Use C-M-s or C-M-r to search for a regular expression instead of a string.

If you want an overview of all the occurrences of a string (or more generally a regular expression), M-x occur is your friend. It shows a list of matching lines in a separate buffer. In the *Occur* buffer, press RET or the middle mouse button to jump to the corresponding line in the original buffer. See the manual for commands that search multiple buffers.

3
  • 2
    If you want a fancier interface helm-swoop may be what you want.
    – rlazo
    Commented Sep 23, 2014 at 21:20
  • 2
    @rlazo You should expand on this comment and make it an answer. Explain what Helm is and the benefits (and downsides) of helm-swoop over built-in methods. Commented Sep 23, 2014 at 21:23
  • When using isearch, M-s o (isearch-occur) will drop you in an Occur buffer showing all matches for the current search.
    – YoungFrog
    Commented May 4, 2016 at 20:29
10

Instead of just providing a link to the manual section section about searching, the real "the manual is your friend" answer is to tell you how to look this up in the manual yourself: C-h r i searching RET or C-h r i search TAB ing RET.

If someone is really at this basic a level with Emacs, then the real question is how to look something up in the manual, and the answer is C-h r i.

Emacs is self-documenting. Start by asking Emacs, not Stack Exchange. To learn to ask Emacs, start with C-h C-h.

4

Press Ctrl+S (isearch-forward), then type your text. Emacs will search as you type. To find the next occurrence, press Ctrl+S again. To go to previous occurrence, press Ctrl+R. To stop, press Enter ↵ to leave the cursor there. Or type Ctrl+G to return to the spot before search was started.

This command is also under the menu Edit.

Ctrl+S twice will search your last searched word.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.