Vim Tips Wiki
Tags: Visual edit apiedit
No edit summary
Tag: sourceedit
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=610
 
|id=610
Line 12: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
You can use Vim's autocomplete feature in insert mode. Just edit the vimrc file and add lines:
+
Vim can expand abbreviations entered while in insert mode. An abbreviation is defined by adding a line like the following to your [[vimrc]]:
 
<pre>
 
<pre>
iab <key> <expansion>
+
iabbrev <keys> <expansion>
<key> is the letter which should be expanded to <expansion>
 
 
</pre>
 
</pre>
   
 
Replace <keys> with the letter or letters which should be expanded to <expansion>, for example:
Example (add your own words):
 
 
<pre>
 
<pre>
iab #i #include (typing "#i" and space will be expanded to "#include")
+
iabbrev #i #include (typing "#i" and space will be expanded to "#include")
iab #d #define (typing "#d" and space will be expanded to "#define")
+
iabbrev #d #define (typing "#d" and space will be expanded to "#define")
iab s struct (typing "s" and space will be expanded to "struct")
+
iabbrev s struct (typing "s" and space will be expanded to "struct")
iab t typedef ( typing "t" and space will be expanded to "typedef")
+
iabbrev t typedef (typing "t" and space will be expanded to "typedef")
 
</pre>
 
</pre>
   
 
In some cases Vim expands a letter automatically that you don't want. You have to watch out for that.
 
In some cases Vim expands a letter automatically that you don't want. You have to watch out for that.
   
  +
When entering a word which is already in the buffer, Ctrl-P or Ctrl-N can be used to [[Any word completion|autocomplete]] the word currently being entered. Autocompletion does not require abbreviations to have been defined, but it only works for a word consisting of characters matching the 'iskeyword' option.
==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.
 
   
  +
If wanted, [[Dictionary completions|dictionary completion]] can used to define words that can be autocompleted. For example, with the following settings, Ctrl-P or Ctrl-N will work with words defined in the specified dictionary file:
----
 
Use a dictionary file:
 
 
<pre>
 
<pre>
set complete+=k
+
:set complete+=k
set dictionary+=/your/dict/file
+
:set dictionary+=/your/dict/file
 
</pre>
 
</pre>
   
 
==Comments==
Ctrl+N, Ctr+P will now search for completions from that dict file.
 
 
----
 
 
You can put your common typos as abbreviations, for auto correction:
 
You can put your common typos as abbreviations, for auto correction:
 
<pre>
 
<pre>
iab teh the
+
iabbrev teh the
iab seperate separate
+
iabbrev seperate separate
 
</pre>
 
</pre>
   
Line 50: Line 44:
 
> How cut the space from the resulted substitution?
 
> 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.
+
From {{help|abbreviations}}: An exception to this is the character <C-]> (Ctrl-]), which is used to expand an abbreviation without inserting any extra characters.
   
 
Example:
 
Example:
 
<pre>
 
<pre>
:ab hh hello
+
:iabbrev hh hello
 
"hh<Space>" is expanded to "hello<Space>"
 
"hh<Space>" is expanded to "hello<Space>"
 
"hh<C-]>" is expanded to "hello"
 
"hh<C-]>" is expanded to "hello"
Line 62: Line 56:
 
Use getchar() to eat up that space, for example:
 
Use getchar() to eat up that space, for example:
 
<pre>
 
<pre>
iab <t <target name="%"></target><Esc>F%s<c-o>:call getchar()<CR>
+
iabbrev <t <target name="%"></target><Esc>F%s<c-o>:call getchar()<CR>
 
</pre>
 
</pre>
   
Line 74: Line 68:
 
To eat the last space, for example, with:
 
To eat the last space, for example, with:
 
<pre>
 
<pre>
iab did <div id="
+
iabbrev did <div id="
 
</pre>
 
</pre>
   
Line 80: Line 74:
   
 
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.
 
---
 
 
To speed up writing English one can learn several speedword systems like [http://jonaquino.blogspot.in/2007/06/yublin-shorthand-for-speed-writing.html Yublin] or [http://www.thetechnicalgeekery.com/2014/01/dutton-speedwords-shorthand/ Dutton Speedwords]
 
   
 
----
 
----

Latest revision as of 03:48, 25 November 2015

Tip 610 Printable Monobook Previous Next

created 2003 · complexity intermediate · version 6.0


Vim can expand abbreviations entered while in insert mode. An abbreviation is defined by adding a line like the following to your vimrc:

iabbrev <keys> <expansion>

Replace <keys> with the letter or letters which should be expanded to <expansion>, for example:

iabbrev #i #include (typing "#i" and space will be expanded to "#include")
iabbrev #d #define  (typing "#d" and space will be expanded to "#define")
iabbrev s struct    (typing "s" and space will be expanded to "struct")
iabbrev 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.

When entering a word which is already in the buffer, Ctrl-P or Ctrl-N can be used to autocomplete the word currently being entered. Autocompletion does not require abbreviations to have been defined, but it only works for a word consisting of characters matching the 'iskeyword' option.

If wanted, dictionary completion can used to define words that can be autocompleted. For example, with the following settings, Ctrl-P or Ctrl-N will work with words defined in the specified dictionary file:

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

Comments[]

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

iabbrev teh the
iabbrev seperate separate

> How cut the space from the resulted substitution?

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

Example:

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

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

iabbrev <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:

iabbrev 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.