shell

Vim. Saving keystrokes: abbreviations

Welcome to a new issue of this short Vim tutorial!

As we saw in previous posts, Vim is the perfect tool if you want to save time in your daily work. It helps you in the write-compile-debug cycle, it indents and autoindents code, it is extremely powerful for searching, replacing and many other frequent tasks.

In this post I will introduce an interesting feature meant to make you type less: abbreviations.

Abbreviations are, as their name indicates, a way to create an association between a short set of characters and a piece of text (or a command).

Abbreviations can be used in insert, replace or command mode. The keywords used to work with abbreviations are all the same, but adding a proper prefix if necessary (i for insert mode, c for command mode and ! for both).

iab        Create abbreviation for theinsert mode
cab        Create abbreviation for the command mode

Recently, I had to write a dockbook template using perl and the Mason library and found myself typing continuously the following thing:

$data->[0]->{}

and between the curly braces the hash key. After writing that twice I got bored of doing it (yep, I am a lazy guy) so I created the following abbreviation:

iab dh $data->[0]->{!cursor!}:call search('!cursor!', 'b')cf!

It associates the “dh” characters with the $data->[0]->{!cursor!}:call search(‘!cursor!’, ‘b’)cf! string. This string writes “$data->[0]->{}” and places the cursor between the curly braces (do not worry about the “call search” stuff, the example could have been iab dh $data->[0]->{} ) By the way, abbreviations are not recursive, so you will never end up stuck in an infinite loop.

Since writing a tag-like document is a real pain, I decided to abbreviate some of the common tags I was using the most, so I created the following abbreviations:

iab fp <formalpara><CR><LF></formalpara>
iab p <para></para>
iab il <itemizedlist><CR><LF></itemizedlist>
iab li <listitem></listitem>
iab e <entry><CR><LF></entry>
iab t <title></title>
iab it <informaltable><CR><LF></informaltable>
iab r <row><CR><LF></row>

<CR> and <LF> have the common meanings (Carriage Return and Line Feed respectively). They were very convenient. You can copy them to your .vimrc file.

But what if you do not want an abbreviation to be expanded?

Suppose we have the following abbreviation:

iab to Total

and we want to write the following sentence:

"I went to Paris"

As soon as you type the space after “to”, the abbreviation will be expanded and you will end up with:

"I went Total "

To avoid this problem, press Ctrl-V after the problematic abbreviation and then continue typing normally.

To disable an abbreviation permanently you can use “unabbreviate”. For instance:

:unabbreviate dh

“abbreviate” lists the current abbreviations. E.g:

i  t             Total
i  b             back

The first column shows the mode in which the abbreviate is available, the second shows the abbreviation and the third one the text it expands to.

Abbreviations are a convenient way of saving some (or possibly a lot of) work, specially if it is repetitive.

Vim Sheet(VII)

  • [i/c/!]ab Create an abbreviation in the specified mode
  • unabbreviate Deletes an abbreviation
  • abbreviate List available abbreviations

1 Comment

  • On 09.04.10 commandliners » Vim. Key mapping said:

    [...] detail. If you need further information, you will need a good Vim manual. As it happens with abbreviations, maps can be used regardless of the mode you are in Vim or they can be restricted to a certain [...]

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*Required Fields