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.
- 22,318
- 4
- 56
- 92
-
2
-
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. – Gilles 'SO- stop being evil' Sep 23 '14 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 May 04 '16 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.
- 77,472
- 10
- 114
- 243
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.
- 1,608
- 2
- 14
- 20
- 7,194
- 10
- 42
- 63
yourmouse over the upvote arrow, what does it say? You opinionated stupid asshole. – towry Oct 26 '18 at 08:15