Newbie Tip: isearch Word Yank

Our newbie tips are our way of expressing emacs features we might otherwise take for granted. We hope they help beginners climb the learning curve faster. This particular tip is one that I wish I knew far sooner when I first started using emacs.

Incremental searching with isearch-forward and isearch-backward (C-s and C-r) is crucial in day-to-day and even minute-by-minute emacs usage. Maybe you want to find all uses of a particular variable name, or maybe you want to make sure you’re not repeating the same word in a piece of literature. Let’s dive in with an example:

Suppose we want to search for the word “lorem” in the folowing paragraph:

We could type C-s l o r e m, but seeing as our point is already at one particular instance of “lorem,” we can instead enter C-s C-w. The result is the same. When in isearch-mode, C-w yanks the next word in the buffer and appends it to the end of the current search string:

Now suppose we want to search for instances of “lorem ipsum.” Just hit C-w again:

Awesome, we just saved 9 key presses — a productivity boost.

C-w is part of the isearch-mode-map. There are many isearch modifiers. Here are some that I use most frequently:

C-s – to search again forward

C-r – to search again, but backward

C-w – to yank next word or character in buffer onto the end of the search string, and search for it

M-y – to yank last killed text onto end of search string and search for it

M-r – to toggle regular-expression mode

M-e – to edit the search string in the minibuffer

For a full listing of available isearch modifier keys, review the help for isearch (C-h f isearch-forward). It is also possible to add your own functions to the isearch-mode-map. Last month, we referenced a way to add occur to isearch.

me likes this site so much! Although I can visit the info document from time to time, this site is surely more fun to read. The article simply become more attractive when you put some neat pictures in.