vi editing  «Prev  Next»
Lesson 4 Creating text abbreviations
ObjectiveUse the :ab command to abbreviate text.

Creating text abbreviations for vi in UNIX

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:
1) In vi, begin the :ab command, which drops your cursor to the status line.
1) The attached image shows a Telnet session with the `vi` editor open. Here's the **output displayed in the console**:
We
formed in the summer of 1998,
and we debuted a few months
later at Talent & Tapestry,
San Jose's annual arts and
crafts fair.
~
~
~
~
~
~
:ab tsh Town Square Harmonizers

Summary:
  • The user has typed a paragraph of text in `vi`.
  • At the bottom, the `:ab` command is being entered:
  :ab tsh Town Square Harmonizers
  

This defines an abbreviation so that typing `tsh` in insert mode will automatically expand to `Town Square Harmonizers` when followed by a space or punctuation.
In vi, begin the :ab command, which drops your cursor to the status line. Continue by typing the abbreviation tsh, followed by its definition, Town Square Harmonizers.

2) After entering the :ab command, continue editing the file. In text mode, change the word.
2) After entering the :ab command, continue editing the file. In text mode, change the word.
Although the status line is now inactive, the ab: command continues to appear until the status line is redrawn ( by another ex command, for example)

3) After typing tsh and then a space, the text abbreviation is triggered.
3) After typing tsh and then a space, the text abbreviation is triggered.

4) Return to command made by pressing Escape.
4) Return to command made by pressing Escape. Then enter :w to save your changes. As a result, the status line changes to display the file name, quartet, followed by the number of lines and characters in the file.

5)By using the :ab command without arguments, it lists your current abbreviations. As you can see, there is only one.
5) By using the :ab command without arguments, it lists your current abbreviations. As you can see, there is only one. The first column, .tsh is the internal name and is always the same as the second column tsh, which lists the abbreviation.

6) If you quit vi, you lose any abbreviations that you defined during the session. You also can turn off abbreviations without quitting vi,
6) If you quit vi, you lose any abbreviations that you defined during the session. You also can turn off abbreviations without quitting vi, as show here. In this example:unab tsh is used to temporarily disable the abbreviation.

In the next lesson, you will learn how to create keyboard macros with the :map command.
SEMrush Software 4 SEMrush Banner 4