I’ve released a new plugin for the Vim
text editor, called "add-to-word-search" (
Scripts page), and I’d like to introduce it here. If you like Vim,
please let me know what you think by commenting below.
In order to properly introduce the plugin, one first should introduce the
different (and useful) commands of Vim of * and #. What they
do is search forward or backward for the complete word under the cursor (or
somewhat before it or after it). Bram Moolenaar (the creator of Vim)
covers them in his “Seven
habits of effective text editing” document (there’s also a
and I think I covered them in a previous Vim tip.
Now, here is the use case that often bugged me: sometimes I searched for
a certain function, found it in the text and then found a function that called
this function (or often in the case of C code, a preprocessor macro that
wrapped it), and wanted to look for its occurrences as well as those
of the previous term. I wasn't aware of any good way to do it, so I ended up
writing the “add-to-word-search” plugin.
After installing it, and after having searched for a word using *
or #, one can press \** to search forward for an additional
word under the cursor (or \## to search backward), and then use it more
times to add additional words.
After publishing this plugin and mentioning it on
issue that it gives an error if you have set nowrapscan. I fixed
it, but was only able to do so by temporarily disabling nowrapscan,
and then enabling it if it was previously enabled. (Apparently, vimscript’s
exception-handling cannot handle some of the built-in errors.)
I also demonstrated it to my (now former) co-worker, who had been trying to
get used to Vim, and he said it looked useful, but asked if there was an easy
way to remove terms from the search query (which there is not at the moment),
and I noted it may be a good idea.
Anyway, this Vim plugin is open-source and available under the MIT/X11 licence.