Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 ยท Basics and Navigation

Incremental Search and Query Replace

Search as you type with C-s and C-r, use regexp, word and symbol search, list matches with occur, and replace text interactively with M-% and C-M-%.

Search is the fastest way to move in a file, and Emacs has an unusually good one. Incremental search (isearch) starts finding matches as soon as you type the first character, so you stop typing the moment the cursor is where you want it. A closely related tool, query replace, changes text one match at a time with your approval.

Keys Command Effect
C-s isearch-forward Search forward
C-r isearch-backward Search backward
C-M-s isearch-forward-regexp Forward regexp search
C-M-r isearch-backward-regexp Backward regexp search

Press C-s, type buffer, and Emacs highlights the first match after point and moves there. Keep typing to narrow it. Then:

  • C-s again jumps to the next match; C-r to the previous.
  • RET ends the search and leaves point on the match.
  • C-g cancels and returns to where you started. (The first C-g removes characters that did not match; a second one aborts.)
  • DEL removes the last typed character and steps back.
  • C-s C-s at the start of a search repeats the previous search string.
  • M-p and M-n browse earlier search strings.
  • C-w pulls the word at point into the search string; C-M-w removes characters from it.
  • M-c toggles case sensitivity. By default searches are case-insensitive unless you type an uppercase letter.
  • M-r toggles regexp mode while searching, M-s w switches to word mode and M-s _ to symbol mode.
  • Any command that is not part of search (like C-a) ends the search and runs.

A very quick way to look up an identifier: put the cursor on it, press C-s C-w, then C-s to hop between uses. M-s . does it in one step: it starts a search for the symbol at point.

JS Stand-in: incremental search

Press Run (or Ctrl + Enter) to see the console output here.

No output.

C-M-s interprets your input as an Emacs regular expression. The syntax is similar to others, with a few differences: groups and alternation need backslashes in Emacs, written \( \) and \|. For example, C-M-s \(cat\|dog\) RET finds the next "cat" or "dog". \b is a word boundary and [0-9]+ matches digits. M-x re-builder opens a helper that highlights matches as you type a pattern.

Showing all matches: occur

M-s o (occur) asks for a regexp and opens a buffer listing every matching line, each with its line number. Click a line, or move to it and press RET, to jump to the spot. With C-u 2 M-s o, each match comes with two lines of context. Even better: you can edit text in the *Occur* buffer after pressing e, and the changes are applied to the source buffer. During an isearch, M-s o runs occur with the current search string.

Query replace

M-% (query-replace) asks for a string to find and a string to replace it with, then goes through the buffer from point, asking for each match:

Key Meaning
y or SPC Replace this one and go on
n or DEL Skip this one
! Replace this and all the rest without asking
. Replace this one and stop
q or RET Stop now
^ Go back to the previous match
E Edit the replacement for this match
? Show all the options

C-M-% (query-replace-regexp) does the same with regexps. In the replacement, \& stands for the whole match and \1 for the first group, so replacing \(\w+\), \(\w+\) with \2 \1 swaps "Smith, John" into "John Smith". Case is smart: if you replace foo with bar and the match was Foo, the replacement becomes Bar. M-x replace-string and replace-regexp do the same without asking. If a region is active, replacement is limited to it.

After !, you can undo the whole batch with a single C-/. Always scan the result after a large replacement.

JS Stand-in: query-replace with scripted answers

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Other ways to jump

  • M-s h r highlights a regexp in the buffer; M-s h u removes the highlights.
  • M-x flush-lines deletes lines matching a regexp and M-x keep-lines deletes the others.
  • M-x grep or M-x rgrep searches many files and gives you a clickable list. Project searches get their own treatment in the version control and tools chapter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you search forward incrementally?

    C-s starts isearch-forward; C-r searches backward.
  2. 2. You pressed C-s and typed a word. How do you abort and return to where you started?

    C-g cancels isearch and restores point; RET keeps point at the match.
  3. 3. Which key replaces all remaining matches in query-replace without asking?

    ! replaces this and every later match without asking.
  4. 4. How do you write alternation in an Emacs regexp?

    Emacs regexps use backslash-pipe, \|, and backslash parentheses for groups.

Summary

  • C-s and C-r search incrementally; RET stays, C-g returns; C-w grabs the word at point.
  • C-M-s searches with a regexp; Emacs regexps use \(, \) and \|.
  • M-s o lists matching lines in an editable *Occur* buffer.
  • M-% and C-M-% replace interactively (y, n, !, ., q); \1 and \& refer to matches.

Next: The Minibuffer and M-x