14

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
nowox
  • 25,978
  • 39
  • 143
  • 293

3 Answers3

15

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
romainl
  • 186,200
  • 21
  • 280
  • 313
8

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

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

François
  • 7,988
  • 2
  • 21
  • 17
  • 1
    If you want to use asterisk instead of F8: `nnoremap * :let @/ = '\<'.expand('').'\>'\|set hlsearch` – Llopeth Jun 05 '18 at 09:08
3

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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324