sponsor Vim development Vim logo Vim Book Ad

basic Tip #11: Completing words quicky in insert mode

 tip karma   Rating 416/133, Viewed by 8754 

created:   February 24, 2001 18:01      complexity:   basic
author:   Yegappan      as of Vim:   5.7


In Insert mode, press the Ctrl-p or Ctrl-n key to complete part of
a word that has been typed.

This is useful while typing C programs to complete long variable and
function names.  This also helps in avoiding typing mistakes.

Note that using the 'complete' option, you can complete keywords
defined in one of the include files, tag file, etc.

To get more help on this, use

:help i_Ctrl-N
:help i_Ctrl-P
:help ins-completion
:help complete

 rate this tip  Life Changing Helpful Unfulfilling 

<<Jumping to previosuly visited locations in a file | Converting tabs to spaces >>

Additional Notes

[email protected], August 17, 2001 14:26
Unfortunately, this can also propagate errors, so use it carefully. I've had times when I've misspelled a keyword or variable somewhere and then CTRL-P/N completed it five times before I noticed what I was doing. (-: Of course, at least that gives you a consistent misspelling that's easy to fix with :%s...

I have found myself performing a lot of CTRL-P/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. This is a lot of fun, especially when you have non-VIM-using co-workers who think you're controlling the editor with your mind as well as your keyboard. (-;
[email protected], September 19, 2002 13:08
I shook with excitement after reading this......
dof@spamsocks-codepoets-foo-co-dot-uk, November 8, 2002 7:52
excitement ? is that all ? I've been using vim for a couple fo years now and have only just found out about this... clearly I need to RTFM more.
[email protected], November 28, 2002 5:52
It's great and making life easier.
Balu, January 17, 2003 15:12
How would I add multiline abbrevs? I've tried to edit them using ^V with no luck
[email protected], February 13, 2003 4:27
Awesome !!!!
Anon, March 13, 2003 22:41
Balu asked about multiline abbreviations?

ab mul Mult^V<Enter>iple lines

the above will probably look like "ab mul Mult^Miple lines".

Anonymous, March 15, 2003 5:08
If you've several words you'd like to complete, you can use CTRL-X CTRL-P to extend the match. e.g.

a line exists in your file:
$res = mysql_query($q) or die(mysql_error());

you want to type the line:
$result = mysql_query($q) or die(mysql_error());

but don't want to have to type it in again. (BTW if you wanted the same line use CTRL-X CTRL-L to complete whole lines)

type
$result = my

keep pressing CTRL-P until you get to mysql_query

then CTRL-X CTRL-P

will give you
$result = mysql_query($q

then keep pressing CTRL-X CTRL-P to complete all the words.

Have a play.

see:
:h i_CTRL-X_CTRL-P
:h i_CTRL-X_CTRL-N


Anonymous, April 28, 2003 11:30
If you want to complete the entire line:
$result = mysql_query($q) or die(mysql_error());

just type:
$result = mys<C-X><C-L>
[email protected], June 4, 2003 2:09
is it possible to set the cursor position in ab ?

i think of:
ab fu (#cursorPos)<Enter>{<Enter>    return 0;<Enter>}
[email protected], July 4, 2003 8:16
The autocompletion is great (I i-mapped it to <Tab>), but the abbreviation "ab" is at least as good!
Thanks!
[email protected], October 30, 2003 11:59
Well, cursor position can be done like this:

ab ff for (Iterator i =; i.hasNext()) {<LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT>

I've tried added a [count] before the <LEFT> but it doesn't seem to work.

Anyone else have an idea?
[email protected], January 9, 2004 11:23
It would be great if we could have something like wildmode=longest,list in insert mode:

A lot of my friends think that the way VIM rotates through possible completions is annoying. We would prefer the behavior to act like the default tab-completion in BASH, where you type <tab> once, and if it can complete with only one match, the completion is performed. If there are other possible completions, then an alphabetical list of completions is listed, giving you an opportunity to type the next letter to further narrow your completion list.

Can anyone suggest how to do this?
Anonymous, March 23, 2004 3:55
Can you put a library file that you somehow include in vim when it starts. The file could for example include all the html cyntax. So can do keyword completion from that file.
Anonymous, May 10, 2004 16:15
I looked some into that bash completion deal, but I don't think it will work based on the way vim finds the completion.  First it searches buffers, then tags, then included files (this is default with complete on--see :help complete), and completes with the first thing it finds.  So to implement a bash-like completion, you would have to parse all buffers, all tags, and all includes, then build a list of matches.  Imagine the "no match found" search time every single time you went to tab-complete a word.  Personally, I'll take the less-cool but faster approach ;)
[email protected], June 17, 2004 6:50
For the question about getting the cursor to a certain position, I find that there are 2 ways to do this:

1) ?ab<enter>   #Where ab is the letters at the position. Its just the typical search backwards but in this case it would go right to it.

2) Nb  #Where N is the number of words you want to go back. 7b will take you back 7 words. Love it.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo