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

How do I set up automatic text expansions that trigger as I type in Insert mode?

Answer

:iabbrev {abbr} {expansion}

Explanation

:iabbrev (Insert-mode abbreviation) defines a short trigger that automatically expands into longer text when you type it followed by a space, Enter, or punctuation. It is Vim's built-in text expansion / snippet system — no plugins required.

How it works

:iabbrev teh the

Now typing teh in Insert mode automatically corrects to the .

Common uses

Typo correction:

:iabbrev teh the
:iabbrev recieve receive
:iabbrev dont don't

Code snippets:

:iabbrev ifmain if __name__ == "__main__":
:iabbrev clog console.log();<Left><Left>

Boilerplate:

:iabbrev myemail user@example.com
:iabbrev mysig Best regards,<CR>John Doe

Types of abbreviations

Command Mode
:iabbrev Insert mode only
:cabbrev Command-line mode only
:abbreviate Both Insert and Command-line

Managing abbreviations

  • :iabbrev (no args) — list all Insert-mode abbreviations
  • :iunabbrev {abbr} — remove an abbreviation
  • :iabclear — remove all Insert-mode abbreviations

Tips

  • Abbreviations expand on word boundaries — teh expands but tehran does not
  • Use <C-v> before the trigger character to prevent expansion: type teh<C-v><Space> to keep teh literal
  • For multi-line expansions, use <CR> in the expansion text
  • :noreabbrev prevents recursive expansion if your abbreviation contains another abbreviation
  • Put abbreviations in your ~/.vimrc to make them permanent

Next

How do I run a search and replace only within a visually selected region?