Vim Tips Wiki
(move in comments from 4)
(add "see also")
Line 90: Line 90:
 
</pre>
 
</pre>
 
----
 
----
  +
  +
==See also==
  +
*[[VimTip4|Any word completion]] automatic completion of words
  +
*[[VimTip267|267 Selectively displaying abbreviations]]
  +
*[[VimTip352|352 Disabling cabbrev]]
  +
*[[VimTip481|481 Abbreviations only on shortcut]]
  +
*[[VimTip593|593 Basic postfix abbreviations]]
  +
*[[VimTip610|610 Use abbreviations for frequently-used words]]
  +
*[[VimTip630|630 Automatically append closing characters]]
  +
*[[VimTip649|649 Expand existing abbreviation]]
  +
*[[VimTip650|650 Abbreviation that prompts whether to expand it or not]]
  +
*[[VimTip842|842 C/C++ function abbreviations]]
  +
*[[VimTip912|912 Smart abbreviation]]
  +
*[[VimTip966|966 Multi-line abbreviations]]
  +
*[[VimTip1071|1071 Simple placeholders]]
  +
*[[VimTip1162|1162 Add closing brace automatically on code blocks]]
  +
*[[VimTip1285|1285 Replace a builtin command using cabbrev]]
  +
*[[VimTip1382|1382 Auto spelling correction using abbreviations]]
   
 
==References==
 
==References==

Revision as of 03:51, 24 March 2010

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

See also

References

Comments