Vim Tips Wiki
Register
Advertisement
Tip 43 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0


Use the :abbreviate command to define abbreviations.

Abbreviations can save typing, and can improve accuracy, when you need to enter the same text throughout your document. You can also create abbreviations to automatically correct common typing errors (such as changing teh to the).

Examples:

:ab rtfm read the fine manual
Whenever you type 'rtfm' followed by punctuation such as a space or comma, the 'rtfm' will be expanded to 'read the fine manual'. This also happens if you type 'rtfm' then press Esc or Enter.
:ab teh the
Whenever you type the word 'teh', it will be replaced with 'the'.
:ab
List all abbreviations. A flag is shown in the first column: 'i' means the abbreviation will be used in insert mode, 'c' for command-line mode, and '!' for both modes.
:una rtfm
:unabbreviate – remove 'rtfm' from the list of abbreviations.
:abc
:abclear – remove all abbreviations.

Note: To avoid expansion in insert mode, type Ctrl-V after the last character of the abbreviation (on Windows, type Ctrl-Q instead of Ctrl-V).

Rough merge in from 992 (now removed)[]

The 'helpgrep' command is very useful for searching through all the files located in the .vim/doc directory. This command is especially useful if your .vim/doc directory is littered with script documentation as well as language specific documentation such as provided by script#614, script#826 or script#1330.

The following abbreviation provides faster access to 'helpgrep'.

:cnoreabbrev H helpgrep

This allows me to type :H uganda to search for the word 'uganda' in all of the help files.

Rough merge in of comments from 4[]

I have found myself performing a lot of Ctrl-P/Ctr-N commands on the same strings, and when this happens, I generally add an abbreviation, which saves me a keystroke or two. I do a lot of programming in ColdFusion, and I have these lines in my .vimrc:

ab Attr Attributes
ab Appl Application
ab Vari Variables
ab Req Request
ab CFQ CFQUERY
ab CFO CFOUTPUT
...

Vim will finish the word as soon as you type a character after the abbreviation.


Example of a multiline abbreviation:

:ab mul Multiple<CR>lines

To move the cursor to a certain position after the abbreviation, try one of these:

?ab<enter>
Where ab is the letters at the position you want (search backwards).
Nb
Where N is the number of words you want to go back. For example, 7b will take you back 7 words.

To get a C-style comment when you type 'com', you can add this to your .vimrc file:

iab com /*<CR><CR>/<Up>

which will expand to:

/*
 * <here-is-the-cursor-position>
 */

Here are some useful abbreviations for Java code:

abbr psvm public static void main(String[] args){<CR>}<esc>O
abbr sysout System.out.println("");<esc>2hi
abbr sop System.out.println("");<esc>2hi
abbr syserr System.err.println("");<esc>2hi
abbr sep System.err.println("");<esc>2hi

abbr forl for (int i = 0; i < ; i++) {<esc>7hi
abbr tryb try {<CR>} catch (Exception ex) {<CR> ex.printStackTrace();<CR>}<esc>hx3ko
abbr const public static final int

abbr ctm System.currentTimeMillis()
abbr slept try {<CR> Thread.sleep();<CR>}<esc>hxA catch(Exception ex) {<CR> ex.printStackTrace();<CR>}<esc>hx3k$hi

Automatically add abbreviations in a file[]

If you wish to enter your abbreviations automatically in a file, without the need to open it, then use the following function :

"------------------------------
"mapping to automatically add an
"abbreviation
fun AddAbbr()
    normal byw
    let StringChar = lh#visual#selection()
    "where lh#visual#selection() is a function you have to download from here :
    "https://github.com/LucHermitte/lh-vim-lib/blob/master/autoload/lh/visual.vim
    "and then, to put in your .vim/autoload directory.
    let val = input("Enter the abbreviation you wish to use for '" . StringChar . "' :")
    exec "ia" val StringChar
    silent call SaveAbbr(StringChar, val)
endfun

fun SaveAbbr(val, abbr)
    redir >>~/.vim/foo.txt
    "foo.txt is the file in which you wish to add your abbreviations. For me, it
    "is ~/.vim/ftplugin/tex.vim
    echo "iab" a:abbr a:val
    redir END
endfun

vmap <S-F8> <ESC>:call AddAbbr()<CR>
"Replace S-F8 by any other shortcut you wish

Copy it in your .vimrc file, and read the comments. Basically, you have to download the file located here and to copy it in the relevant directory. Then, select a word or an expression in visual mode, and push Shift+F8.

Another way to vim abbreviations to manage multiline abbreviations, snippets, and skeleton files[]

Save the snippets in a directory in plain text file. Next, write an abbreviation command like this.

iab text <ESC>:read /path/to/snippet/text_expansion.txt<CR>i

Explanation:

  • <ESC> - Go to normal mode
  • :read - Read from text file from /path/
  • The text_expansion.txt will contain a multiline or single line text that is used to expand text.
  • <CR> - To execute the read command
  • i - go back to insert mode
  • Press <space> after text to expand and place a space after. Or use C-] to expand without space.

This way, you can avoid using any other snippet manager plugins.

See also[]

References[]

Comments[]

Advertisement