How do I set up automatic typo correction in Vim using abbreviations?
Answer
:iabbrev teh the
Explanation
Vim's abbreviation feature lets you define automatic text replacements that trigger as you type. When configured with common typos, it acts as a built-in auto-correct system that fixes mistakes the moment you make them.
How it works
The :iabbrev command (short for insert-mode abbreviation) creates a mapping from a trigger word to its replacement. When you type the trigger word in insert mode and follow it with a space, punctuation, or press Escape, Vim automatically replaces it with the expanded text.
Unlike :imap, abbreviations are word-aware. They only trigger when the abbreviation forms a complete word, so defining teh as the will not interfere when you type words like tehran. The replacement happens at word boundaries.
Abbreviations can also be used as text snippets for frequently typed phrases. The :cabbrev variant works in command-line mode, and :abbrev works in both insert and command-line mode.
Example
Add these to your .vimrc for common typo corrections:
" Fix common typos
:iabbrev teh the
:iabbrev adn and
:iabbrev waht what
:iabbrev tehn then
:iabbrev recieve receive
:iabbrev acheive achieve
:iabbrev seperate separate
" Use as text shortcuts
:iabbrev myemail john@example.com
:iabbrev myaddr 123 Main Street, Springfield
:iabbrev shrug ¯\_(ツ)_/¯
To see all current abbreviations, run :abbreviate. To remove a specific one, use :iunabbrev teh. To clear all abbreviations, use :abclear.
A useful technique is combining abbreviations with <CR> for multi-line expansions using the <C-o> trick. However, for more complex snippet functionality, a dedicated snippet plugin like UltiSnips would be more appropriate.