Vim Tips Wiki
m (TODO: Fix typo ("quicky" to "quickly") in title)
(Fix help links)
Line 17: Line 17:
 
To get more help on this, use
 
To get more help on this, use
   
:help i_Ctrl-N
+
{{help|i_CTRL-N}}
:help i_Ctrl-P
+
{{help|i_CTRL-P}}
:help ins-completion
+
{{help|ins-completion}}
:help complete
+
{{help|'complete'}}
:help i_CTRL-X_CTRL-P
+
{{help|i_CTRL-X_CTRL-P}}
:help i_CTRL-X_CTRL-N
+
{{help|i_CTRL-X_CTRL-N}}
   
 
}}
 
}}

Revision as of 15:19, 24 July 2007

Previous TipNext Tip

Tip: #11 - Completing words quickly in insert mode

Created: February 24, 2001 18:01 Complexity: basic Author: Yegappan Version: 5.7 Karma: 675/208 Imported from: Tip#11

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'
:help i_CTRL-X_CTRL-P
:help i_CTRL-X_CTRL-N

Comments

TODO: Merge with VimTip4
TODO: I just noticed the typo - Title should be "quickly" not "quicky".


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^V<Enter>lines

The ^V means you press Ctrl-V (Ctrl-Q on Windows).


If you've several words you'd like to complete, you can use Ctrl-X Ctrl-P to extend the match.
For example, suppose 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());

Type:
$result = my

Keep pressing Ctrl-P until you get to mysql_query.

Then pressing Ctrl-X Ctrl-P will give:
$result = mysql_query($q


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.
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 /*^M^M/<Left><Up>

which will expand to:

/*
 * <here-is-the-cursor-position>
 */

To get bash-like completion, where only the longest common sequence is written use

:set completeopt+=longest

Here are some useful abbreviations for Java code:

abbr psvm public static void main(String[] args){^M}^[ko
abbr sysout System.out.println("");^[hhi
abbr sop System.out.println("");^[hhi
abbr syserr System.err.println("");^[hhi
abbr sep System.err.println("");^[hhi

abbr forl for(int i=0; i<; i++){^[hhhhhhi
abbr tryb try{^M} catch(Exception ex){^M ex.printStackTrace();^M}^[hxkkko
abbr const public static final int

abbr ctm System.currentTimeMillis()
abbr slept try{^M Thread.sleep();^M}^[hhxA catch(Exception ex){^M ex.printStackTrace();^M}^[hhxkkkA^[hi

The special characters ^M and ^[ need to be entered as:

^M : Ctrl-V then Ctrl-M (Ctrl-Q then Ctrl-M on Windows)
^[ : Ctrl-V then Escape (Ctrl-Q then Escape on Windows)