19.4 Interactive Highlighting by Matching
It is sometimes useful to temporarily highlight text that
matches a certain regular expression. For example, you might wish to
see all the references to a certain variable in a program source file,
highlight certain parts in a voluminous output of some program, or
make certain names stand out in an article.
Use the M-x hi-lock-mode command to turn on a minor mode that
allows you to interactively add and remove regular expressions
specifying text to be highlighted. Hi Lock mode works like Font Lock
mode (see Font Lock), except that it lets you easily add and
remove regular expressions while you are editing a buffer. To enable
Hi Lock mode for all buffers use M-x global-hi-lock-mode or
place (global-hi-lock-mode 1)
in your .emacs file.
You control Hi Lock mode with these commands:
- C-x w h regexp <RET> face <RET>
- Highlight text that matches regexp using face face
(
highlight-regexp
). By using this command more than once, you
can highlight various parts of the text in different ways. The
highlighting will remain as long as the buffer is loaded. For
example, to highlight all occurrences of the word “whim” using the
default face (a yellow background) C-x w h whim <RET>
<RET>. Any face can be used for highlighting, Hi Lock provides
several of its own and these are pre-loaded into a history list. While
being prompted for a face use M-p and M-n to cycle through
them.
- C-x w r regexp <RET>
- Unhighlight regexp (
unhighlight-regexp
).
When activated from the menu select the expression to unhighlight from
a list. When activated from the keyboard the most recently added
expression will be shown. Use M-p to show the next older
expression and M-n to select the next newer expression. When
the expression to unhighlight appears press <RET> to unhighlight
it. The expression can also be typed and completion is available.
- C-x w l regexp <RET> face <RET>
- Highlight entire lines containing a match for regexp, using face
face (
highlight-lines-matching-regexp
).
- C-x w b
- Insert all the current highlighting regexp/face pairs into the buffer
at point, with comment delimiters to prevent them from changing your
program. This key binding runs the
hi-lock-write-interactive-patterns
command.
These patterns will be read the next time you visit the file while
Hi Lock mode is enabled, or whenever you use the M-x
hi-lock-find-patterns command.
- C-x w i
- Re-read regexp/face pairs in the current buffer
(
hi-lock-write-interactive-patterns
). Users familiar with Font
Lock keywords might interactively enter patterns
(highlight-regexp
), write them into the file
(hi-lock-write-interactive-patterns
), edit them, perhaps
including different faces for different parenthesized parts of the
match, and finally use this command
(hi-lock-write-interactive-patterns
) to have Hi Lock highlight
them.
This command does nothing if the major mode is a member of the list
hi-lock-exclude-modes
.