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 forwardC-r– to search again, but backwardC-w– to yank next word or character in buffer onto the end of the search string, and search for itM-y– to yank last killed text onto end of search string and search for itM-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.
3 responses so far ↓
I’ve been using emacs for 10 years and didn’t know about C-w, and more importantly, M-y.
It always frustrated me that I couldn’t paste something into the search string. I was always using C-y.
This web site has been a wake up call for me. I got comfortable w/ my base set of emacs knowledge and I stopped learning.
Thanks again guys
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.
C-w, here I come.
I missed learning about C-w also, and I’ve been using emacs for years. Thanks for a great tip.
Leave a Comment