Vim Masterclass Part #3: Abbreviations

This next post in the ‘Vim Masterclass’ will focus on a handy feature of Vim called Abbreviations. Abbreviations are a string of text which when typed are transformed into another string of text. This is done with the :abbr command. The format is:

:abbr text newtext

So, an example of this would be:

:abbr fl ForLinux

When in insert mode in Vim, type ‘fl’ followed by a space, and watch it expand. The abbreviation must be followed by a space so Vim knows that the word has finished and isn’t part of a bigger word.

To see the current abbreviations, use the command:

:ab

To remove an abbreviation, use the :una command, followed by the abbreviation:

:una fl

To remove all abbreviations, use:

:abc

Abbreviations are especially useful when combined with some of Vims other control codes. A line break can be added to the abbreviation with the <CR> code:

:abbr php <?php<CR>?>

When ‘php’ is typed into the document, it outputs:
<?php
?>

Unfortunately, the cursor is now at the end of the php tags. To counter that, the <Up> control can be added to the end of the output which will move the cursor after the abbreviation:

:abbr php <?php<CR><CR>?><Up>

This will now output:

<?php

?>

With the cursor placed on the line between the two tags.

The following abbreviation offers a html 5 skeleton outline, leaving Vim in insert mode within the title tag in the header waiting for your input:

:abbr html <!DOCTYPE html><CR><html><CR><head><CR><title></title><CR></head><CR>
<body><CR><CR></body><CR></html><esc>5<Up>2<Right>i

This entry was posted in Managed Hosting. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

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