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?

link|improve this question

40% accept rate
feedback

2 Answers

up vote 2 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.

Edit:

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

:nmap <silent> * :let @/='\<'.expand('<cword>').'\>'<CR>
link|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
feedback

Quick hack:

:nnoremap * *N

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.