Vim: search and highlight but do not jump

后端 未结 12 2121
一生所求
一生所求 2020-12-02 18:02

The super star (*) key in Vim will search for the word under the cursor and jump forward to the next match. The user can jump to the next matches with the n key.

12条回答
  •  萌比男神i
    2020-12-02 18:54

    The other answers here are good, particularly @rodrigo's, but I wanted to write a solution that preserves scroll position and does so without affecting any of the marks.

    This works for me:

    function! StarPositionSave()
      let g:star_position_cursor = getpos('.')
      normal! H
      let g:star_position_top = getpos('.')
      call setpos('.', g:star_position_cursor)
    endfunction
    function! StarPositionRestore()
      call setpos('.', g:star_position_top)
      normal! zt
      call setpos('.', g:star_position_cursor)
    endfunction
    nnoremap  * :call StarPositionSave()*:call StarPositionRestore()
    

    Putting normal! * in the function directly doesn't seem to work, as (at least in neovim) it suppresses search highlighting from being triggered (as if :nohlsearch was run).

提交回复
热议问题