In the `vi` editor (and its enhanced version `vim`), the `:ab` (short for abbreviate) command lets you define custom text abbreviations. When you type a short word and then press a non-keyword character (like space or Enter), `vi` expands it to a longer phrase or command.
🔹 Syntax
:ab abbr full_form
abbr
is the short abbreviation.
full_form
is what it expands to.
✅ Example Usage
:ab hw Hello, world!
Now, in insert mode, when you type:
hw␣
…it automatically becomes:
Hello, world!
🔹 View All Abbreviations
To list all defined abbreviations:
:ab
🔹Remove an Abbreviation
To remove a specific abbreviation:
:unab hw
🔹 Use Case in UNIX Editing
Let’s say you're often editing configuration files and need to repeatedly type:
export PATH=$PATH:/usr/local/bin
You could define:
:ab ep export PATH=$PATH:/usr/local/bin
Now typing `ep␣` in insert mode will expand the abbreviation automatically.
Because editing can be repetitive, you may find yourself typing common phrases over and over again. For example, I sing in a quartet named Town Square Harmonizers, but it is inconvenient to constantly type our name in a file. I can avoid repeatedly typing the name if I use the
:ab
command to save a sequence of text as an abbreviation, like this:
:ab tsh Town Square Harmonizers
From now on, whenever I am in text mode and I type the 'word'
tsh
, vi automatically expands it to Town Square Harmonizers. I say 'word' because abbreviations are not expanded until you type them as you would a regular word--with a space or punctuation mark after the abbreviation. The table below shows the general form of the related abbreviation commands:
Command | Description |
:ab short long | Define short as an abbreviation for long |
:ab | List your currently defined abbreviations |
:unab short | Turn off the definition for short |
The following
series of imagesshows some examples of text abbreviations: