Emacs Advent Calendar 10: Searching
Thu 2015-12-10
Emacs of course has commands to search for occurences of strings.
The normal mode of operation is an incremental search which can be
performed forwards (C-s
) or backward (C-r
). Those two
commands prompt for a string to search. Repeated invocations of either
C-s
or C-r
jump to the next or previous match
respectively. Note that you can change direction in mid-search.
Nearly all other commands, especially RET
and all movement
commands, cancel a search. To repeat a canceled search with the last
search string, press C-s C-s
or C-r C-r
.
Searches start from point. If a search fails at the end of a buffer,
another press of C-s
starts the search again from the
beginning of the buffer. Emacs displays Wrapped
in the echo
area. If the search passes the original location of point, Emacs will
display Overwrapped
.
To include a literal newline in the search string, enter it by
pressing C-j
.
To edit the search string, press M-e
. To get back to an
earlier search string, use M-n
and M-p
to cycle
through the search ring (similar to the minibuffer command ring).
Entries from the kill ring can be inserted to the search string by
yanking them with C-y
.
A very handy feature when searching for words that might be separated
by a newline character (e.g. in texts that are filled to a width of N
characters) is word search. To start word search or to toggle word
search mode from an active incremental search, press M-s w
.
To switch from incremental searching to an interactive search and
replace operation, press M-%
(more on this later).
There are some special rules for searching one should be aware of to not be surprised in cases where this is not the intended behavior:
- Incremental search performs lax space matching, i.e. each sequence
of spaces in the search string matches any sequence of one or more
spaces in the text. This can be toggled by
M-SPC
. - If a search string contains only characters in lower case, matching
will be performed case insensitive. To toggle case sensitivity
during an incremental search, press
M-c
.
For more information see sections 15.1 (Incremental Search), 15.3 (Word Search) and 15.9 (Searching and Case) of the Emacs Reference Manual.
Tags: emacsThis text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.