How do I set up automatic text expansions or typo corrections in insert mode?
Answer
:iabbrev
Explanation
:iabbrev defines insert mode abbreviations — short strings that expand automatically when you type a non-keyword character (like space or Enter) after them. Unlike mappings, abbreviations are word-boundary aware, so they only trigger when typed as standalone words. This makes them ideal for typo correction, boilerplate expansion, and personal shorthand.
How it works
:iabbrev lhs rhs
lhsis the abbreviation to typerhsis the expanded text it becomes- The expansion triggers after you type any non-keyword character (space, Enter, punctuation) following
lhs - Unlike
inoremap, abbreviations won't fire in the middle of a word —tehonly expands if it's a standalone word, not insidetether
Example
In your ~/.vimrc:
iabbrev teh the
iabbrev improt import
iabbrev @@ your@email.com
iabbrev retunr return
Now in insert mode, typing teh (teh followed by space) automatically corrects to the . The cursor remains positioned naturally for continued typing.
Tips
- Use
<C-]>in insert mode to trigger an abbreviation manually without typing a space :iabbrev <buffer> ...creates buffer-local abbreviations — useful inFileTypeautocmds for language-specific expansions:iabclearremoves all insert mode abbreviations;:iunabbrev {lhs}removes one- View all abbreviations with
:iabbrev(no arguments) - For multi-line expansions, use
<CR>in the rhs::iabbrev ifmain if __name__ == '__main__':<CR>