Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

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.

Edit:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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