Wikia

Vim Tips Wiki

Changes: Use abbreviations for frequently-used words

Edit

Back to page

(adjust previous/next navigation + minor manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 77: Line 77:
 
</pre>
 
</pre>
   
In insert mode, type <tt>did</tt> then press Ctrl-]
+
In insert mode, type <code>did</code> then press Ctrl-]
   
 
Result: The abbreviation is expanded with no extra characters, and you are still in insert mode and can continue typing.
 
Result: The abbreviation is expanded with no extra characters, and you are still in insert mode and can continue typing.

Latest revision as of 05:38, July 13, 2012

Tip 610 Printable Monobook Previous Next

created 2003 · complexity intermediate · version 6.0


You can use Vim's autocomplete feature in insert mode. Just edit the vimrc file and add lines:

iab <key> <expansion>
<key> is the letter which should be expanded to <expansion>

Example (add your own words):

iab #i #include (typing "#i" and space will be expanded to "#include")
iab #d #define (typing "#d" and space will be expanded to "#define")
iab s struct (typing "s" and space will be expanded to "struct")
iab t typedef ( typing "t" and space will be expanded to "typedef")

In some cases Vim expands a letter automatically that you don't want. You have to watch out for that.

CommentsEdit

Similarly, in insert mode you can hit Ctrl+P or Ctrl+N to autocomplete. Ctrl+P searches upward in your text for what your trying to complete to, Ctrl+N searches forward in your text. After exhausting the current buffer, both of these commands will begin searching other open buffers. I'm not sure, but I believe that there is also a search path you can specify in the .vimrc if you wish.


Use a dictionary file:

set complete+=k
set dictionary+=/your/dict/file

Ctrl+N, Ctr+P will now search for completions from that dict file.


You can put your common typos as abbreviations, for auto correction:

iab teh the
iab seperate separate

> How cut the space from the resulted substitution?

From :help abbreviations: An exception to this is the character <C-]>, which is used to expand an abbreviation without inserting any extra characters.

Example:

:ab hh hello
    "hh<Space>" is expanded to "hello<Space>"
    "hh<C-]>" is expanded to "hello"

Use getchar() to eat up that space, for example:

iab <t <target name="%"></target><Esc>F%s<c-o>:call getchar()<CR>

Regarding how to eat the last typed character (when it is a space): Use :Iabbr and :Inoreabbr from script#50.


See the SuperTab plugin. It does almost all of this without the need for programming.


To eat the last space, for example, with:

iab did <div id="

In insert mode, type did then press Ctrl-]

Result: The abbreviation is expanded with no extra characters, and you are still in insert mode and can continue typing.


Around Wikia's network

Random Wiki