Photo courtesy of dantesz
This is a guest post written by SathiyaMoorthy.
Enabling thesaurus option for vi / vim will help you to choose alternate words from thesaurus. For example, while typing “important”, you may prefer to use a different word than “important”. Wouldn’t be nice if vi can give you few alternative words such as: “valuable” or “substantial” or “significant”?
This article explains how to use vi / vim effectively by enabling thesaurus option in three steps.
Step 1: Define a Thesaurus file
All the relevant words should be grouped together on a single line with either comma or space delimited. For example, you can create your own thesaurus file as shown below for the word “important”.
$ vim /home/jsmith/mythesaurus.txt important,valuable,substantial,significant
Step 2: Specify Thesaurus File Location in ~/.vimrc
Add the following line to .vimrc specifying the location of thesaurus file.
$ vim ~/.vimrc set thesaurus+=/home/jsmith/mythesaurus.txt
Step 3: Use Thesaurus While Editing Document Using Ctrl x + Ctrl t
From vim, if you want to use an alternative word, press Ctrl x + Ctrl t in the insert mode. For example, when you’ve typed the word “important”, press Ctrl-x and Ctrl-t, which will show a popup with the alternate words “valuable”, “substantial” and “significant” as shown below from your /home/jsmith/mythesaurus.txt file.
Download and Use a Pre-defined Thesaurus
Instead of defining your own custom thesaurus, download and use the pre-defined famous moby thesaurus as shown below.
$ wget http://www.gutenberg.org/dirs/etext02/mthes10.zip $ unzip mthes10.zip Archive: mthes10.zip inflating: aaREADME.txt inflating: roget13a.txt inflating: mthesaur.txt
Use mthesaur.txt as the thesaurus file. It is quite large and you would get more than 50 related words for each word.
Add the following line to .vimrc to specify the location of the mthesaur.txt thesaurus file.
$ vim ~/.vimrc set thesaurus+=/home/jsmith/mthesaur.txt
How can a programmer use thesaurus feature in vim?
This can be very helpful for programmers. For example, a PHP programmer can create a php-functions file with the following lines and specify this as thesaurus file inside the ~/.vimrc.
$ vim /home/jsmith/php-functions.txt math abs acos acosh asin asinh atan atan2 atanh base_convert bindec ceil cos errors debug_backtrace debug_print_backtrace error_get_last error_log error_reporting restore_error_handler
Add the php-functions.txt to .vimrc specifying the location of thesaurus file.
$ vim ~/.vimrc set thesaurus+=/home/jsmith/mythesaurus.txt set thesaurus+=/home/jsmith/mthesaur.txt.txt set thesaurus+=/home/jsmith/php-functions.txt
Now, when you type “math” in your PHP file and press Ctrl x and Ctrl t, all the PHP math functions will be displayed. Also, please note that you define multiple thesaurus files as shown above.
Recommended Reading
Learning the Vi and Vim Editors, by Arnold Robbins. I’m a command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to carry the Vi editor pocket reference with me all the times. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.
This article is part of the ongoing Vi Tips and Tricks series. Please subscribe to TheGeekStuff and don’t miss any future Vi and Vim editor tips and tricks.
This article was written by SathiyaMoorthy, developer of EPQA. Sathiya got impressed by the 15 Examples To Master Linux Command Line History article on this blog and became a regular reader and contributor. The Geek Stuff welcomes your tips and guest articles.
Comments on this entry are closed.
Awesome.
Amazing stuff 😉 But I’m not really sure in wich case I can use this feature, but great!
@mk_michael
Thanks for your comment & appreciation.
The theme of this article is to publish about the two practical usages, one is for a programmer and other is for every body.
1. Programmer can use it for, navigating through the relevant function names using ctrl x + ctrl t.
2. Non programmer can use it for, while editing the document, mail and other text related stuffs and use newer English words in it.
If you have some other practical usages let us know about it.
Great tip. I really like your website: you take te time to explain and to illustrate the tips 🙂 Thanks for the time that you spend on these tasks 🙂
@Gilles,
Thanks for the very nice compliment about The Geek Stuff website. We try hard to deliver high-quality articles on an on-going basis. We are always very happy and excited to read these nice compliments.
Nice tip, thanks for the link to a thesaurus.
Using 7.2, I get a “tag stack empty” error when I try to use it with C-x, C-t. I’m reluctant to go and create a huge tags file given the size of mthesaur.txt and that I’ll only be using for text files, which is only a small number of times compared to doing programming. I don’t want that 10M of tags memory being used when I’m writing python code.
Anything else I should have done to get this working?
Nick– you need to be in insert mode.
An alternative is to use this plugin:
http://www.vim.org/scripts/script.php?script_id=2528
To enable the plugin on Debian/Ubuntu:
sudo aptitude install python-nltk openoffice.org-thesaurus-en-us
The tip on how to use the moby thesaurus in vi was excellent!
Is there a way to also implement an English dictionary? I find myself using vi more and more for all sorts of editing tasks, and with a dictionary (and thesaurus) implemented, I could easily give up stardict (and all its dependencies) and use vi from console for all my writing/editing needs.
Many thanks !! 🙂
Hi,
I tried this and i get a pattern not found after trying the ctrl-x ctrl-t. Any ideas?
I did the steps in the tutorial but I have like 10000+ matches for the word important?? do you know why?