Posted by Swaroop C H on August 25, 2007 Filed under: python, tech, vim

Most of my writing inside the browser is done with the combination of the It's All Text extension and Vim. During this writing, I use words whose meaning I know vaguely but don't know the exact usage, and in such cases, I usually open a new browser window and look up the meaning of the word in an online dictionary. This process of looking up the dictionary meaning was getting rather tedious, so I wrote a small Vim plugin today to do this job with a simple command.

To use the plugin, first install BeautifulSoup. Then, get my lookup.vim script and copy it to ~/.vim/plugin/.. See the new version's installation instructions, it now uses John Goerzen's dict client implementation in Python.

Next time you use Vim, just place your cursor over any word and run :Lookup, and you'll see something like this:

lookup.vim screenshot

Since, this is the only command starting with L on my Vim installation, I just end up running :L. Of course, you can always create your own keyboard shortcuts to make it easier.

Update: Based on the comments, I've updated the script to now use the DICT protocol and talks to some servers. This avoids screen-scraping, is much faster to use, allows me to now use both a dictionary and a thesaurus which is very very handy because I can see what other words I can use, and of course, doesn't violate any TOS (which screen-scraping could amount to).


Michael Watkins says:

And, if your client is *nix, you can ignore wordnet and all and simply use dict at the command line. In vim:

function! LookupDictionary()
let theword = expand("")
exec ':!dict ' . theword

" Look up a word using dict
nnoremap :call LookupDictionary()
inoremap :call LookupDictionary()

Just another WordPress weblog says:

[...] Swaroop C H, The Dreamer » Python: Lookup [...]

Swaroop says:

@Sridhar: Thanks, didn't know about that.

@Michael: Nice idea, although I tend to be on Windows too. Perhaps, there are binaries for dict on gnuwin32.sf.net

@Kartik: Should be easy :)

@Abhijit, @Chris: Enjoy!

Kartik Mistry says:

Cool. I will love to write similar plugin for GujaratiLexicon.com.

srid says:

By using wordnet, you can obviate internet connection and html scraping,

Abhijit Nadgouda says:

Good one, trying it out.

Chris says:

This comment has just been written with "It's All Text" and gvim, thanks for mentioning the former. :-)

finferflu says:

Hi, I've just donwloaded your cool plugin, but I was unable to lookup words. Then I found out that the server indicated in the code is currently not working, so I have edited it to dict.org, and I also had to change moby-thesaurus to moby-thes, this way (I hope BBCode is on):

conn = dictclient.Connection('dict.org')

output += "n".join([d.getdefstr() for d in conn.define('wn', word)]) # WordNet
output += "nn"
output += "n".join([d.getdefstr() for d in conn.define('moby-thes', word)]) # Moby Thesaurus

It would be nice if you could update the plugin on the Vim website :)

Swaroop says:

@finferflu : Thanks for pointing that out, and I've updated the plugin on the vim website :)

finferflu says:

Cool, thank you for your work :)


There's no comment box, but please do email me or tweet me your thoughts and criticisms, and I will publish the relevant ones here.