Emacs: Search / Highlight Words

By Xah Lee. Date: . Last updated: .

This page shows you how to use search & highlight features in emacs.

The most useful one is “isearch”.

emacs main search command
keycommand
Ctrl+sisearch-forward
Ctrl+risearch-backward
emacs isearch 2014-06-13
emacs isearch. 【Ctrl+s

While in isearch:

How to make case sensitive in isearch?

By default, search is not case sensitive unless your search string contains Capital Letter.

While in isearch, press 【Alt+s c】 to toggle search case-sensitivity.

Or, call toggle-case-fold-search before isearch. Remeber to change it back when you are done. You should give it a easy key. 〔➤see Emacs: How to Define Keys

Make Space Hyphen Lowline Equivalent

You can set isearch so that typing space will also search for - and _ and newline.

;; make isearch treat space dash underscore newline as same
(setq search-whitespace-regexp "[-_ \n]")

Search Word Under Cursor

While in isearch, press 【Ctrl+w】 to select more strings to the right of cursor.

The following commands do isearch but uses the word the cursor is on, saves you typing.

emacs search commands
keycommandpurpose
Alt+s .isearch-forward-symbol-at-pointSearch the “symbol” under cursor, with boundary check. That is, “xyz” does not match “xyz2”. What chars are part of “symbol” depends on major mode. 〔➤see Emacs 24.4 New Features (released 2014-10)
Alt+s wisearch-forward-wordsearch the word under cursor, disregard differences of {underscore _, hyphen -, space }, but also with boundary check. That is, “x y” does not match “x y2”.
Alt+s _isearch-forward-symbolSame as isearch but with boundary check.

WARNING:

  1. These isearch commands do boundary check. If current word is “xy”, it will not find “xy2”.
  2. Whether underline _ or hyphen - is part of “symbol” or “word” depends on current syntax table, which is usually different in different major modes.

Emacs: isearch Current Word

For a command to isearch current word without the above problems, see:

Emacs: isearch Current Word

isearch Keys/Modes

isearch has many complex features. You can read about them by calling:

List Matching Lines

Sometimes you want to list the lines matching a regex.

emacs highlight commands
keycommandpurpose
Alt+s ooccursame as list-matching-lines

You can also delete matching lines, sort lines, …. See: Emacs: List/Delete/Highlight Matching Lines, Sort/Reverse Lines.

Highlighting Commands

isearch will color matched strings, but when you exit isearch, the coloring will go away.

The following will highlight until you close the file.

emacs highlight commands
keycommand
Alt+s h .highlight-symbol-at-point
Alt+s h fhi-lock-find-patterns
Alt+s h lhighlight-lines-matching-regexp
Alt+s h phighlight-phrase
Alt+s h rhighlight-regexp
Alt+s h uunhighlight-regexp

Right Click to Highlight Words

Emacs Lisp: Setup Mouse Click to Highlight Matching Words

Search Text in Files

Emacs: Search Text in Files (grep)

Find/Replace

Emacs: Find & Replace

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from my keyboard store.