Vim Editor: How to Correct Spelling Mistakes Automatically

by Ramesh Natarajan on March 12, 2009

Vim Automatic Typo Correction
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.

  1. One method is to to teach Vim about the words that you typically make mistakes, and the correct spelling for it.
  2. 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

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 / 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.

Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. Vi and Vim Autocommand: 3 Steps to Add Custom Header To Your File Automatically
  2. Vi and Vim Editor: 3 Steps To Enable Thesaurus Option
  3. Vi and Vim Editor: 5 Awesome Examples For Automatic Word Completion Using Ctrl-X Magic
  4. 8 Essential Vim Editor Navigation Fundamentals
  5. Vim Editor: How To Setup Most Recently Used Documents Features using MRU Plugin
  

Vim 101 Hacks Book

{ 5 comments… read them below or add one }

1 Himanshu Sheth March 12, 2009 at 11:58 am

Very interesting, it would be very much useful; especially in Comments section.\

Have digged and Delicious(ed) it.

Liked your 101 Hacks as well.

-Himanshu Sheth

2 David Engel March 12, 2009 at 12:50 pm

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/).

3 frequser March 12, 2009 at 10:25 pm

Is it possible to do all these abbreviations, in windows? notepad or wordpad? kindly suggest.

4 Anthony Panozzo March 12, 2009 at 10:27 pm

Hi Sathiya,

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 panozzaj@gmail.com or contribute to the github repository at http://github.com/tjweir/vim-autocorrect/tree/master. Thanks!

5 Narendra.s.v March 14, 2009 at 4:56 am

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 .

Leave a Comment

Previous post:

Next post: