xralf
xralf

Reputation: 3642

Go to the next same word in Vim without using search

Is there a shortcut in Vim for going to the next word which is the same as the word cursor is on? It should work without typing the word with search command /.

Upvotes: 17

Views: 5678

Answers (5)

ngm
ngm

Reputation: 7457

gd and its variants are also very handy. It works slightly differently from *, in that it searches for the local declaration of the word under the cursor (or global declaration if none local exists). You can then search through the results with n like you can with superstar, and it will skip comments. It can be helpful when you're editing code.

:help gd

Upvotes: 5

Randy Morris
Randy Morris

Reputation: 40927

While all the answers here are correct, I thought it may be useful to provide a little more info.

What * actually does is perform a forward search for \<word-under-the-cursor\>. Because this is just a search operation, you can then navigate forwards and backwards to the next occurrences using n and N. This also means your previous search is lost. # is exactly the same as * except it performs a reverse search.

The \< and \> in the search string are word boundaries in vim's regex language which is what makes this work so nicely. It's also important to note that what is considered a "word" is determined by the iskeyword option. See :help word for more information.

Upvotes: 8

JJJ
JJJ

Reputation: 33163

* goes to the next matching word and # goes to the previous matching word. * is so useful it's sometimes called the super star.

Upvotes: 41

Rajesh Pantula
Rajesh Pantula

Reputation: 10241

To search the current word under the cursor use '*'.

to search backwards for current cursor word use '#'

Upvotes: 5

Pavan Manjunath
Pavan Manjunath

Reputation: 28525

* and # are your friends ( forward and backward directions respectively )

Upvotes: 9

Related Questions