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?
3 Answers
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.
-
2If you want a fancier interface helm-swoop may be what you want.– rlazoCommented 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. Commented May 4, 2016 at 20:29
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
.
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
mouse over the upvote arrow, what does it say? You opinionated stupid asshole.