M-x all-things-emacs

Newbie Tip: isearch Word Yank

March 26th, 2007 by Ryan McGeary · 3 Comments

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:

Step 1: Incremental Search Yank

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:

Step 2: Incremental Search Yank

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

Step 3: Incremental Search Yank

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.

Tags: isearch · newbie · quick · tips

3 responses so far ↓

  • 1 Jeff Squires // Mar 26, 2007 at 2:26 pm

    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

  • 2 Yang Zhao // Mar 26, 2007 at 3:01 pm

    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.

  • 3 Mike Buksas // Mar 26, 2007 at 4:44 pm

    I missed learning about C-w also, and I’ve been using emacs for years. Thanks for a great tip.