5

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?

2 Answers 2

7

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>
1
  • 1
    The first solution has problems when there is only single match per file. Second one cause screen blinking. But third one is perfect. Thanks Jun 20, 2011 at 18:12
0

Quick hack:

:nnoremap * *N

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

You must log in to answer this question.

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