Vim selects a word with * without moving the cursor

I would like to highlight a word under the cursor without moving on to the next result.

The solution I found uses a marker:

noremap * mP*N`P 

Is there a better solution?

I noticed that vim is not good at keeping its cursor position after executing commands. For example, when I switch the buffer, the cursor returns to the beginning of the line.

I may be interested to have global settings for this.

+6
source share
3 answers

Unfortunately, there are no global settings for this.

One common solution to the “highlight without movement” problem is:

 nnoremap * *`` 

One solution to the "keep 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 
+10
source

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

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

+4
source

My SearchHighlighting plugin modifies the * command so that it does not move on to the next match; you can still jump by setting [count] . He also extends this command in visual mode (another frequently requested feature not in Vim) and a few more.

+2
source

Source: https://habr.com/ru/post/969371/


All Articles