Photo courtesy of shandrew
This article is written by SathiyaMoorthy.
This article is part of the ongoing Vi / Vim Tips and Tricks Series. I tend to make simple spelling mistakes while writing, as English is not my first language. Wouldn’t be nice if Vim editor can do the spelling correction automatically as you make a typo?
Note: I wish someone from
Nayv Navy football team read this article. Oh well, that would’ve still not fixed their problem.
In this article, let us review how to correct spelling mistakes automatically inside Vim editor.
There is a facility in VIM which can abbreviate/expand words for you. For example, when you type US, it can automatically be converted to United States. Add the following line to the ~/.vimrc to define the abbreviation.
$ vim ~/.vimrc iab US United States
Once you’ve defined the above, anytime you type US inside Vim, it will be abbreviated to United States.
Vim can automatically correct your spelling mistakes while you are typing using one of the following methods.
- One method is to to teach Vim about the words that you typically make mistakes, and the correct spelling for it.
- Another method is to use a plugin that already has all the common spelling mistakes, and the correct spelling for it.
Method 1: Vim Spelling Correction – Using Custom Defined Words
Many advanced users might already be using the abbreviation technique for spelling correction. For example, add the following line to the ~/.vimrc. So when you wrongly type “teh”, it will be converted into “the” automatically.
$ vim ~/.vimrc iab teh the
Whenever you find yourself making the same typo repeatedly, add it in to ~/.vimrc
$ vim ~/.vimrc iab word_with_wrong_spelling correct_word
Method 2: Vim Spelling Correction – Using AutoCorrect.vim Plugin
The autocorrect.vim plugin has a collection of all typical spelling mistakes and it’s correct spelling.
Author of the Plugin, “Anthony Panozzo” describes about the plugin as: “Correct common typos and misspellings as you type”
Following are few examples from the autocorrect.vim plugin.
ia Britian Britain ia Brittish British . . ia Acceptence Acceptance ia Acceptible Acceptable ia accesories accessories
2 steps to Install the autocorrect.vim plugin
Step 1: Download the autocorrect.vim plugin
Download the plugin from vim.org website.
$ cd ~ $ wget -O autocorrect.tar http://www.vim.org/scripts/download_script.php?src_id=9900 $ tar xvf autocorrect.tar
Step 2: Install autocorrect.vim plugin
From Vim, you can execute “:source /path/to/the/autocorrect.vim” whenever needed (or) For permanent usage, add the following line to ~/.vimrc
$ vi ~/.vimrc :source ~/autocorrect.vim
After doing this, when you make a typo of a word that is in the autocorrect.vim list, it will be corrected automatically.
$ vi test-typo.txt thsi is acceptible [Note: Above line will automatically change to "this is acceptable"]
When you want the expansion or spelling mistake correction to be stopped for a particular word, you can do the following inside vim. This is a temporary unabbreviation. If you want this to be permanent, remove the word either from ~/.vimrc or ~/autocorrect.vim.
$ vim test-typo.txt :una US
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 / Vim 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, author of numbertotext Vim plugin, which will replace the numbers with the equivalent text inside Vim. The Geek Stuff welcomes your tips and guest articles.
Comments on this entry are closed.
Very interesting, it would be very much useful; especially in Comments section.\
Have digged and Delicious(ed) it.
Liked your 101 Hacks as well.
I use Vim for writing — diary, etc. — and coding, so one useful function I’ve found for autocorrect is from Anthony Panozzo (http://22ideastreet.com/blog/2008/11/06/vim-word-processing/).
Is it possible to do all these abbreviations, in windows? notepad or wordpad? kindly suggest.
Just wanted to say thanks for including my plugin on your list of ways to correct with Vim! I really appreciated your emails in the past to talk about this before you posted. I thought that was very professional.
If anyone has any corrections to the corrections they might want included in future versions of the plugin, feel free to email me at email@example.com or contribute to the github repository at http://github.com/tjweir/vim-autocorrect/tree/master. Thanks!
Wow!!! Nice and useful information. I have been using Vim editor for a long time and i didnt know this feature.Thanks for sharing this .
the autocorrect plugin only works for VIM ver 6.0 not the later 7 series
@elwoode: I have only tested on the 7+ series, so it seems like this should be good. What problems are you having?
On a different note, the github repo is now at https://github.com/panozzaj/vim-autocorrect.
Hope this helps!
got it working fine, not sure I will have it running by default though as it really slows the startup of Vim.
Yeah, I generally try to source it when needed like in the article. Once I had it autoload and was on a slower computer and it took awhile. Good idea!