History Report a problem
Article Edit this page Discussion

Completing words quickly in insert mode

From Vim Tips Wiki

(Redirected from VimTip11)
Jump to: navigation, search
 

[edit] Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 11Previous TipNext Tip

Created: February 24, 2001 Complexity: basic Author: Yegappan Minimum 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.

[edit] References

[edit] Comments

 

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)

Better use <CR> for Enter and <ESC> for Escape. Can be copy-pasted from web.


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Completing words quickly in insert mode"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation