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

How do I create text abbreviations that auto-expand while typing in Vim?

Answer

:iabbrev {abbr} {expansion}

Explanation

How it works

The :iabbrev command creates abbreviations that automatically expand when you type them in insert mode. When you type the abbreviation followed by a non-keyword character (like a space, punctuation, or Enter), Vim replaces it with the full expansion.

The syntax is:

:iabbrev {abbreviation} {replacement text}

Abbreviations are triggered only when the abbreviation forms a complete word. If you type the abbreviation as part of a larger word, it will not expand.

To remove an abbreviation, use :iunabbrev {abbr}. To list all insert-mode abbreviations, use :iabbrev with no arguments.

Example

Fix common typos automatically:

:iabbrev teh the
:iabbrev adn and
:iabbrev recieve receive

Now whenever you type teh in insert mode, it instantly becomes the .

Create shortcuts for frequently typed text:

:iabbrev @@ your.email@example.com
:iabbrev ssig Best regards,<CR>John Doe
:iabbrev clog console.log();<Left><Left>

You can also use abbreviations for boilerplate code:

:iabbrev ifmain if __name__ == "__main__":

Tips

  • Add abbreviations to your ~/.vimrc to make them permanent.
  • Use :abbreviate (without the i prefix) to create abbreviations that work in both insert mode and command-line mode.
  • To insert the abbreviation literally without expansion, press Ctrl-V before the character that would trigger the expansion.
  • Abbreviations are different from mappings. Abbreviations expand after you finish typing the word, while mappings trigger as soon as the key sequence is typed.

Next

How do you yank a single word into a named register?