Search code examples
vimhighlighttext-cursor

Vim highlight a word with * without moving cursor


I would like to be able to highlight the word under cursor without going to the next result.

The solution I found uses a marker:

noremap * mP*N`P 

Is there any better solution ?

I noticed vim is not really good to keep the position of its cursor after the execution of commands. For instance, when I switch buffer, the cursor go back to the begining of the line.

I might be interesting to have a global setting for this.


Solution

  • There's unfortunately no global setting for that.

    One common solution to the "highlight without moving" problem is:

    nnoremap * *``
    

    One solution to the "keep the cursor position when switching buffers" problem is:

    augroup CursorPosition
      autocmd!
      autocmd BufLeave * let b:winview = winsaveview()
      autocmd BufEnter * if(exists('b:winview')) | call winrestview(b:winview) | endif
    augroup END