how to extend search-for-word-under-cursor

11 views
Skip to first unread message

Richard Mitchell

unread,
Sep 29, 2020, 4:05:28 PM9/29/20
to vim_use
Can a search-for-word-under-cursor be extended to search
for both that word and the same word with a specific leading
character?

For example, when sitting on "foo", I want to find both "foo"
and "_foo", but not anything else.

Tim Chase

unread,
Sep 29, 2020, 4:24:56 PM9/29/20
to Richard Mitchell, vim...@googlegroups.com
I don't do it regularly, but a couple ideas for you to play with.

1) relax your requirements such that "g*" and "g#" will also find
these matches (":help g*"). However, they find any match, even
non-whole-word matches

2) Pull the current word into a manual search:

/\<_\=<c-r><c-w>\>

This does a search requiring that a word start here ("\<"), has an
optional ("\=") leading underscore, then pulls in the Word under
the cursor with control+R followed by control+W, then finally
requires that the word end here ("\>"). This can be wrapped up in
a mapping (as long as you use proper escaping) if you do it
frequently enough to warrent. Something like this untested

:nnoremap <f2> /<bslash><lt>=<c-r><c-w><bslash>><cr>

might do the trick.

-tim




Richard Mitchell

unread,
Sep 29, 2020, 6:11:56 PM9/29/20
to vim_use
Thanks!

Using ctrl-s instead, this does exactly what I asked:

nnoremap <C-S> /<bslash><lt>_\=<c-r><c-w><bslash>><cr>

However, I may not have asked the right question.

I use various plugins to extend searching, such as:
  vim-scripts/Add-to-Word-Search and inkarkat/vim-SearchHighlighting

Ideally I'd like these to automagically inherit the modified search.
I'm just being greedy.

Thanks again!

Reply all
Reply to author
Forward
0 new messages