Search code examples
vim

Add word under cursor to search pattern


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\>?


Solution

  • 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>