How do I set up auto-correct abbreviations in Vim to fix common typos?
Answer
:ab teh the
Explanation
Vim's abbreviation system automatically expands short text sequences as you type, making it perfect for auto-correcting typos, inserting boilerplate snippets, or expanding shorthand into full text. When you type the abbreviation followed by a non-keyword character (like space, enter, or punctuation), Vim instantly replaces it with the expansion.
How it works
:ab {abbreviation} {expansion}creates an abbreviation that triggers in insert mode and command-line mode:iab {abbreviation} {expansion}creates one that only triggers in insert mode:cab {abbreviation} {expansion}creates one that only triggers in command-line mode- The abbreviation expands when you type a non-keyword character after it (space,
.,,,<CR>, etc.) :una {abbreviation}removes an abbreviation:abwith no arguments lists all current abbreviations
Example
Add common typo corrections to your vimrc:
:iab teh the
:iab recieve receive
:iab taht that
:iab seperate separate
Now whenever you type teh in insert mode, Vim instantly corrects it to the .
Use abbreviations for boilerplate too:
:iab @@ your.email@example.com
:iab ssig Best regards,<CR>John Smith
Typing @@ followed by a space inserts your full email address. Typing ssig followed by a space inserts a two-line signature.
Tips
- Use
<C-]>in insert mode to expand an abbreviation immediately without typing a trailing space - To type an abbreviation literally without triggering expansion, press
<C-v>before the space or trigger character - Abbreviations are whole-word only —
tehinsideatehwill not trigger, preventing false positives - Use
:iab <buffer> {abbr} {expansion}to create buffer-local abbreviations that only apply to the current file - Combine with
autocmd FileTypefor language-specific snippets:autocmd FileType python iab <buffer> ifmain if __name__ == "__main__": - For multi-line expansions, use
<CR>in the expansion::iab forloop for (int i = 0; i < n; i++) {<CR>}<Esc>O - Abbreviations are stored in your vimrc and loaded every session — build up a personal dictionary of corrections over time