Subscribe to the feed

Sysadmins are always looking for ways to optimize their workflow and accomplish more in the time they have. When using a text editor like Vim (and its derivatives), one way you can become more efficient is by saving typing. One of Vim's most overlooked features in this category is Vim abbreviations.

[ Get the Vim cheat sheet. ]

An abbreviation is a Vim feature that replaces a small chunk of text—the abbreviation—with another chunk of text as you type. In most cases, the abbreviation replaces text that is much longer, saving you from typing it all out. For example, you can create an abbreviation that replaces sig with your full email signature.

You generally use abbreviations to replace text you often type, such as signatures, comments, code blocks, or even long words or expressions. You can also use it as a quick autocorrect feature for words you often misspell.

Define an abbreviation

To create abbreviations in Vim, you can use the command :abbreviate (which itself can be abbreviated as :ab) followed by the abbreviation and the text you want to replace it with. For example, to abbreviate the word "operating system" as "os," use the abbreviate command like this:

:ab os operating system

By default, the command :abbreviate creates an abbreviation that works in insert and command modes. You can use variations such as :iab to create an abbreviation that works only in insert mode or :cab for command mode only.

[ Keep your favorite Git commands, aliases, and tips close at hand. Download the Git cheat sheet. ]

Use an abbreviation

To trigger an abbreviation, type the abbreviation word, for example os in the example above, followed by a non-keyword character; for example, you can use the Space bar or Enter.

Unlike mappings, abbreviations trigger only when you type the abbreviation by itself and not in the middle of another word. This is a great option when you're typing text because it's usually what you want. For example, in the example above, Vim will not trigger the abbreviation when you type the word autos or oscar.

To prevent an abbreviation from expanding, type Ctrl+V before entering the abbreviated word.

Remove an abbreviation

Use the command :ab without any parameters to see a list of all abbreviations defined for the current buffer. To remove an abbreviation, use the command :unab followed by the abbreviation or :abc to clear all abbreviations.

Common ways to use Vim abbreviations

Now that you can define and use abbreviations, here are some common ways to use them to gain efficiency.

Autocorrect  mistakes

Use abbreviations to quickly autocorrect frequently misspelled words, such as teh, adn, and tihs:

:ab teh the
:ab adn and
:ab tihs this

Since you can misspell these words on the command line and in insert mode, use :ab to define the abbreviations. Now, as soon as you type one of those misspellings, Vim will correct it for you right away.

Type long text strings

Use Vim abbreviations to type frequently used long strings of text, such as your email signature. You can use special characters; for example, use <CR> to add a new line:

:iab sig Regards,<CR>Ricardo<CR>

Or a comment you often type, like "looks good to me":

:iab lgtm looks good to me

Because you'll almost always use these abbreviations only when typing text and almost never in command mode, use :iab to define an insert mode abbreviation.

Enter code snippets

You can also use abbreviations to define common code blocks for your favorite programming language. For example, define a common if error block for Go programs:

:iab ife if err != nil {<CR><tab>return err<CR>}<CR>

When you type ife, this abbreviation expands to:

if err != nil {
	return err
}

[ Want to test your sysadmin skills? Take a sysadmin skills assessment today. ]

You can also use more complex abbreviations that include cursor movement. For example, the following creates a Go for block, then positions your cursor in insert mode to define the loop's termination condition by adding <esc> to enter normal mode, and uses movement keys to move the cursor to the correct position:

:iab ffi for i := 0; i<; i++ {<CR><CR>}<esc>2k14li

This abbreviation expands to:

for i := 0; i< ; i++ {

}

The cursor is positioned after i< in insert mode, allowing you to type the condition immediately.

You can use the same idea to create an HTML tag, like this:

:iab p <p></p><esc>3hi

Or a markdown code block:

:iab 3~ ~~~<CR><CR>~~~<esc>kI

This abbreviation expands to:

~~~

~~~

This abbreviation was handy when I wrote this article.

Abbreviations are a good way to save time typing in code blocks, but they lack some features. For more complex snippets, try snippet plugins.

Don't overlook abbreviations

Abbreviations are one of Vim's most overlooked features. They are powerful, flexible, and easier to start using than mappings or more advanced features like snippet plugins.

You can define these abbreviations in your Vim configuration file and even associate them with particular file types. For example, you can add Go-related code abbreviations only for Go files.

For more details, check the Vim tips wiki's Vim abbreviations page.


About the author

Ricardo Gerardi is Technical Community Advocate for Enable Sysadmin and Enable Architect. He was previously a senior consultant at Red Hat Canada, where he specialized in IT automation with Ansible and OpenShift. 

He has been a Linux and open source enthusiast and contributor for over 20 years. He is currently interested in hacking stuff using the Go programming language, and he's the author of Powerful Command-Line Applications in Go: Build Fast and Maintainable Tools. Ricardo also writes regularly about Linux, Vim, and command line tools for Opensource.com and Enable Sysadmin community publications.

Ricardo enjoys spending time with his daughters, reading science fiction books, and playing video games.

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

Browse by channel

automation icon

Automation

The latest on IT automation for tech, teams, and environments

AI icon

Artificial intelligence

Updates on the platforms that free customers to run AI workloads anywhere

open hybrid cloud icon

Open hybrid cloud

Explore how we build a more flexible future with hybrid cloud

security icon

Security

The latest on how we reduce risks across environments and technologies

edge icon

Edge computing

Updates on the platforms that simplify operations at the edge

Infrastructure icon

Infrastructure

The latest on the world’s leading enterprise Linux platform

application development icon

Applications

Inside our solutions to the toughest application challenges

Original series icon

Original shows

Entertaining stories from the makers and leaders in enterprise tech