Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 486
nowox Avatar asked May 16 '14 13:05

nowox


3 Answers

My SearchHighlighting plugin changes the * command, so that it doesn't jump to the next match; you can still jump by supplying a [count]. It also extends that command to visual mode (another frequently requested feature not in Vim), and some more.

like image 192
Ingo Karkat Avatar answered Oct 01 '22 14:10

Ingo Karkat


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
like image 14
romainl Avatar answered Oct 17 '22 10:10

romainl


As detailed here, you can define a map as follows:

:nnoremap <F8> :let @/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>

like image 8
François Avatar answered Oct 17 '22 11:10

François