Vim Tips Wiki
(change "anon" author to blank; trim "created" date; minor manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
Tag: rollback
(One intermediate revision by the same user not shown)
Line 3: Line 3:
 
|id=610
 
|id=610
 
|previous=609
 
|previous=609
|next=611
+
|next=613
 
|created=2003
 
|created=2003
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=
 
|author=
|version=5.7
+
|version=6.0
 
|rating=1377/369
 
|rating=1377/369
 
|category1=Abbreviations
 
|category1=Abbreviations
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.

Revision as of 05:38, 13 July 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.

Comments

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.