Vim Recipes Searching Searching for the Word Beneath the Cursor

Searching for the Word Beneath the Cursor

Problem

You want to search the current document for the word underneath your cursor. You'd rather not type it in again.

Solution

In Normal mode, place your cursor on or just in front of the word you wish to find, then press * (mnemonic: star search). This will jump to the next occurrence of the word in the current file. Alternatively, pressing # will find the previous occurrence.

Discussion

After finding the first match using either of the above methods, you can press the n (mnemonic: next match) key to jump to the next match. To jump to the previous match, use N.

To search for words containing the current word, press g* or g#, as appropriate. For instance, if the current word is back, g* will jump to hunchback.