Wikia

Vim Tips Wiki

Changes: Using abbreviations

Edit

Back to page

(Change <tt> to <code>, perhaps also minor tweak.)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
Use the <tt>:abbreviate</tt> command to define abbreviations.
+
Use the <code>:abbreviate</code> 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'').
 
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:
 
Examples:
;<tt>:ab rtfm read the fine manual</tt>
+
;<code>:ab rtfm read the fine manual</code>
 
: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.
 
: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.
   
;<tt>:ab teh the</tt>
+
;<code>:ab teh the</code>
 
:Whenever you type the word 'teh', it will be replaced with 'the'.
 
:Whenever you type the word 'teh', it will be replaced with 'the'.
   
;<tt>:ab</tt>
+
;<code>:ab</code>
 
: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.
 
: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.
   
;<tt>:una rtfm</tt>
+
;<code>:una rtfm</code>
:<tt>:unabbreviate</tt> – remove 'rtfm' from the list of abbreviations.
+
:<code>:unabbreviate</code> – remove 'rtfm' from the list of abbreviations.
   
;<tt>:abc</tt>
+
;<code>:abc</code>
:<tt>:abclear</tt> – remove all abbreviations.
+
:<code>:abclear</code> – 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).
 
'''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).
Line 41: Line 41:
 
</pre>
 
</pre>
   
This allows me to type <tt>:H uganda</tt> to search for the word 'uganda' in all of the help files.
+
This allows me to type <code>:H uganda</code> to search for the word 'uganda' in all of the help files.
   
 
==Rough merge in of comments from 4==
 
==Rough merge in of comments from 4==

Revision as of 05:09, July 13, 2012

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 :
    "http://lh-vim.googlecode.com/svn/vim-lib/tags/2.2.1/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" StringChar val
    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.

See also

References

Comments

Around Wikia's network

Random Wiki