Vim: search and highlight but do not jump

后端 未结 12 2060
一生所求
一生所求 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条回答
  • 2020-12-02 18:42

    :map cc :let @/ = '\<'.expand('<cword>').'\>'\|set hlsearch<C-M>

    0 讨论(0)
  • 2020-12-02 18:45

    Similar to * we have

    [I ..................... it shows where the word under the cursor appears
    

    I also have some useful lines on my vimrc that can, maybe, help you

    " When double click a word vim will hightlight all other ocurences
    " see CountWordFunction()
    " [I shows lines with word under the cursor
    nnoremap <silent> <2-LeftMouse> :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>:CountWord<cr>
    nnoremap <Leader>* :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>:CountWord<cr>
    
    if !exists('*CountWordFunction')
        fun! CountWordFunction()
            try
                let l:win_view = winsaveview()
                exec "%s/" . expand("<cword>") . "//gn"
            finally
                call winrestview(l:win_view)
            endtry
        endfun
    endif
    
    command! -nargs=0 CountWord :call CountWordFunction()
    cnoreabbrev cw CountWord
    nnoremap <F3> :CountWord<CR>
    
    0 讨论(0)
  • 2020-12-02 18:46

    The best solution:

    1. don't add a jump to the jump list
    2. the behavior of the star key is not be changed

    so, try the plugin: http://www.vim.org/scripts/script.php?script_id=4335

    Much better than:

    " a jump adds to the jump list
    nnoremap * *``
    " I got a dead loop on macvim
    nnoremap * :keepjumps normal *``<cr>
    " the behavior is changed
    nnoremap <silent> <Leader>* :let @/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>
    
    0 讨论(0)
  • 2020-12-02 18:49

    I haven't seen this one yet:

    nmap <silent> * "syiw<Esc>: let @/ = @s<CR>

    It's very short and does not involve jumping around which can result in blinking.

    Explanation: copy the word under cursor to s register and then set the search register (/) to the content of s register. The search register is not writeable directly, that's why the let is necessary and hence the silent to keep vim's command line clean.

    0 讨论(0)
  • 2020-12-02 18:49

    I have the following in my .vimrc, which I think works better than the other alternatives:

    " Put word under cursor into search register and highlight
    nnoremap <silent> <Leader>* :let @/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>
    vnoremap <silent> <Leader>* :<C-U>
      \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
      \gvy:let @/=substitute(
      \escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR>
      \gV:call setreg('"', old_reg, old_regtype)<CR>:set hls<CR>
    
    0 讨论(0)
  • 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 <silent> * :call StarPositionSave()<CR>*:call StarPositionRestore()<CR>
    

    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).

    0 讨论(0)
提交回复
热议问题