Vim: search and highlight but do not jump

后端 未结 12 2059
一生所求
一生所求 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:30

    I found this works pretty well, there's no blink and it doesn't need an intermediate register.

    nnoremap <silent> * :let @/= '\<' . expand('<cword>') . '\>' <bar> set hls <cr>
    

    Or if you want the g* behavior:

    nnoremap <silent> g* :let @/=expand('<cword>') <bar> set hls <cr>
    
    0 讨论(0)
  • 2020-12-02 18:32

    My solution:

    nnoremap <silent><expr> * v:count ? '*'
          \ : ':execute "keepjumps normal! *" <Bar> call winrestview(' . string(winsaveview()) . ')<CR>'
    nnoremap <silent><expr> g* v:count ? 'g*'
          \ : ':execute "keepjumps normal! g*" <Bar> call winrestview(' . string(winsaveview()) . ')<CR>'
    

    Pros:

    • No flickering.
    • Jump list remains unchanged.
    • If count is given, it acts like original *.
    • It doesn't use marks or registers.
    • Using actual *, its behavior is almost identical with * (except for jumping).
    • It doesn't need plugin installation.
    0 讨论(0)
  • 2020-12-02 18:35

    I would map:

    nnoremap * *``
    

    Works exactly like you want, except that it adds a jump in the jump list. To prevent that you need:

    nnoremap * :keepjumps normal! mi*`i<CR>
    
    0 讨论(0)
  • 2020-12-02 18:38

    If you want to keep the current view and add the search to the history, try this [not so efficient] solution:

    noremap * msHmt`s*`tzt`s
    

    It is using the marks s (save) and t (top).

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

    A simple solution came to my mind: put map * *# in .vimrc file (it will blink though).

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

    Many answers here outline rather simple mappings that work well for common cases, but may have side effects (like flickering by jumping back and forth) or lack robustness (may break if some regexp characters are defined as keyword characters).

    If you're looking for a robust implementation and don't mind installing a plugin, you can choose from a plethora of alternatives, many of which also offer additional search-related improvements:

    • My SearchHighlighting plugin changes the * command, extends it to visual selections, and offers optional auto-searching of the word under the cursor.
    • star search changes the behavior of * to not jump to the next match, and includes the visual search from the next plugin
    • vim-visual-star-search provides searching of the visual selection
    • visualstar.vim provides searching of the visual selection
    • select & search can use either n/N or * in the visual selection, and can avoid jumping.
    • vim-asterisk provides a z* mapping that also doesn't jump, visual *, more intuitive smartcase handling, and can keep the cursor position when jumping (like ,*)
    • searchant.vim hooks into the built-in search commands and provides a separate highlighting for the match last jumped to.
    0 讨论(0)
提交回复
热议问题