Tip 87Search for the Current Visual Selection

In Normal mode, the * command lets us search for the word under the cursor. Using a small amount of Vim script, we can redefine the * command in Visual mode so that, instead of searching for the current word, it searches for the current selection.

Search for the Current Word in Visual Mode

In Visual mode, the * command searches for the word under the cursor:

KeystrokesBuffer Contents

{start}

 She sells sea shells by the sea shore.

*

 She sells sea shells by the sea shore.

We start off in Visual mode with the first three words selected and our cursor placed on the word “sea.” When we invoke the * command, it searches forward for the next occurrence of the word “sea,” extending the range of the ...

Get Practical Vim, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.