Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In most use cases I use * command just to highlight current word in source code to make sure that it was typed right for example.

How can I make star command to stay on current word?

If that is impossible, how can I bind another key to do that?

share|improve this question
up vote 4 down vote accepted

I think the simplest solution is this mapping:

:nnoremap * *``

where the last two characters are two backticks to restore the cursor to the original position. Another solution would be

:nnoremap * *N

but that moves the cursor to the beginning of the word. Both solutions have the disadvantage that your screen may scroll if the next match is off-screen. That could be fixed with a function--it just depends on the behavior you want and how much work you want to put into it.


Just thought of another simple one that doesn't move the cursor at all.

:nmap <silent> * :let @/='\<'.expand('<cword>').'\>'<CR>
share|improve this answer
The first solution has problems when there is only single match per file. Second one cause screen blinking. But third one is perfect. Thanks – Bogdan Gusiev Jun 20 '11 at 18:12

Quick hack:

:nnoremap * *N

This will perform a backward search after searching for the word under cursor with *.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .