vimtricks.wiki Concise Vim tricks, one at a time.

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
  • lhs is the abbreviation to type
  • rhs is 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 — teh only expands if it's a standalone word, not inside tether

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 in FileType autocmds for language-specific expansions
  • :iabclear removes 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>

Next

How do I define autocmds safely so they don't duplicate when my vimrc is re-sourced?