Add word under cursor to search pattern

后端 未结 2 1403
盖世英雄少女心
盖世英雄少女心 2021-02-12 18:15

Using * when the cursor is on a word theWord, vim directly jumps to the next appearance of exactly that word, i.e. performes /\\

相关标签:
2条回答
  • 2021-02-12 18:30

    Try something like:

    1. * (to search for a word)
    2. move somewhere else
    3. :let @/=@/.'\|\<'.expand("<cword>").'\>' this appends to the previous search pattern the current word under the cursor) with some delimiters (\| and the word boundaries...)
    4. if you want to, set up a hotkey for it, like: nnoremap <F4> :let @/.='\\|\<'.expand("<cword>").'\>'<CR>
    0 讨论(0)
  • 2021-02-12 18:39

    Try

    :nnoremap <silent> + :let @/ .= '\\|\<'.expand('<cword>').'\>'<cr>n
    

    That will append the word under the cursor to the search register when '+' is hit, and jump to the next occurrence of any searched pattern.

    If you wish to extend it to the visual mode, (as it could be done to n_star), you have

    :vnoremap <silent> + <c-\><c-n>:let @/ .= '\\|'.escape(lh#visual#selection(), '/\^$*.[~')<cr>n
    

    With lh#visual#selection() to fetch the current selection, and escape() to neutralize some active characters in regexes. v_CTRL-\_CTRL-N being a safe and silent escape sequence.

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