Add word under cursor to search pattern

一个人想着一个人 提交于 2020-05-10 07:14:54

问题


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

Questions:

Is there a way to add another word otherWord to the search, when the cursor is on this other word, such that one performes /\<theWord\>\|\<otherWord\>?


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/9261123/add-word-under-cursor-to-search-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!