Shlomif's Technical Posts Community - New Vim Plugin: Add to Word Search [entries|archive|friends|userinfo]
Shlomif's Technical Posts Community

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Links
[Links:| Shlomi Fish's Homepage Main Journal Homesite Blog Planet Linux-IL Amir Aharoni in Unicode open dot dot dot ]

New Vim Plugin: Add to Word Search [Apr. 24th, 2012|10:51 am]
Previous Entry Add to Memories Share Next Entry

shlomif_tech

[shlomif]
[Tags|, , , , ]
[Current Location |Home]
[Current Mood |lazylazy]
[Current Music |Lutricia McNeal - Stranded]

I’ve released a new plugin for the Vim text editor, called "add-to-word-search" ( GitHub repository, Vim 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 video available), 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 #vim, “ironcamel” reported an 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. Enjoy!

LinkReply

Comments:
From: (Anonymous)
2012-04-24 12:30 pm (UTC)

Good job buddy

(Link)

I like the Idea! I use Emacs personally and Jedit and can't bear fucking modal editor (no offense) but I like that there is innovation going on in editors!

GNU RULEZ!
[User Picture]From: shlomif
2012-05-27 08:08 pm (UTC)

Re: Good job buddy

(Link)

Hi Anonymous.

Thanks. Something similar should be doable in Emacs and JEdit, as well as most other decent programmer’s editor that are either scriptable and customisable, or alternatively ship with the source.

Regards,

-- Shlomi Fish