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.
Get the Linux Sysadmin Course Now!