Vim Tips Wiki
(Merged VimTip4, VimTip291 and VimTip1257 into this one)
(Little clean up of some comments)
Line 18: Line 18:
 
This is particularly useful when entering the names of variables in a program.
 
This is particularly useful when entering the names of variables in a program.
   
The 'complete' option controls where the keywords are searched (include files, tag files, buffers, ...).
+
The 'complete' option controls where the keywords are searched (include files, tag files, buffers, ...).<br>
 
The 'completeopt' option controls how the completion occurs (for example, whether a menu is shown).
 
The 'completeopt' option controls how the completion occurs (for example, whether a menu is shown).
   
Line 56: Line 56:
 
Example of a multiline abbreviation:
 
Example of a multiline abbreviation:
   
:ab mul Multiple^V<Enter>lines
+
:ab mul Multiple<CR>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.<br>
 
If you've several words you'd like to complete, you can use Ctrl-X Ctrl-P to extend the match.<br>
For example, suppose a line exists in your file:<br>
+
For example, suppose a line exists in your file:
$res = mysql_query($q) or die(mysql_error());
+
$res = mysql_query($q) or die(mysql_error());
 
You want to type the line:
 
 
$result = mysql_query($q) or die(mysql_error());
You want to type the line:<br>
 
 
Type:
$result = mysql_query($q) or die(mysql_error());
 
 
$result = my
 
Type:<br>
 
$result = my
 
 
 
Keep pressing Ctrl-P until you get to mysql_query.
 
Keep pressing Ctrl-P until you get to mysql_query.
   
Then pressing Ctrl-X Ctrl-P will give:<br>
+
Then pressing Ctrl-X Ctrl-P will give:
$result = mysql_query($q
+
$result = mysql_query($q
 
 
----
 
----
 
To move the cursor to a certain position after the abbreviation, try one of these:
 
To move the cursor to a certain position after the abbreviation, try one of these:
 
?ab<enter>
 
?ab<enter>
 
 
:Where ab is the letters at the position you want (search backwards).
 
: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. For example, 7b will take you back 7 words.
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:
 
To get a C-style comment when you type 'com', you can add this to your .vimrc file:
 
iab com /*<cr><cr>/<Up>
 
iab com /*^M^M/<Left><Up>
 
   
 
which will expand to:
 
which will expand to:
 
 
/*
 
/*
 
* <here-is-the-cursor-position>
 
* <here-is-the-cursor-position>
 
*/
 
*/
 
----
 
To get bash-like completion, where only the longest common sequence is written use<br>
 
:set completeopt+=longest
 
   
 
----
 
----
Line 105: Line 88:
   
 
<code><pre>
 
<code><pre>
abbr psvm public static void main(String[] args){^M}^[ko
+
abbr psvm public static void main(String[] args){<cr>}<esc>O
abbr sysout System.out.println("");^[hhi
+
abbr sysout System.out.println("");<esc>2hi
abbr sop System.out.println("");^[hhi
+
abbr sop System.out.println("");<esc>2hi
abbr syserr System.err.println("");^[hhi
+
abbr syserr System.err.println("");<esc>2hi
abbr sep System.err.println("");^[hhi
+
abbr sep System.err.println("");<esc>2hi
   
abbr forl for(int i=0; i<; i++){^[hhhhhhi
+
abbr forl for (int i = 0; i < ; i++) {<esc>7hi
abbr tryb try{^M} catch(Exception ex){^M ex.printStackTrace();^M}^[hxkkko
+
abbr tryb try {<cr>} catch (Exception ex) {<cr> ex.printStackTrace();<cr>}<esc>hx3ko
 
abbr const public static final int
 
abbr const public static final int
   
 
abbr ctm System.currentTimeMillis()
 
abbr ctm System.currentTimeMillis()
abbr slept try{^M Thread.sleep();^M}^[hhxA catch(Exception ex){^M ex.printStackTrace();^M}^[hhxkkkA^[hi
+
abbr slept try {<cr> Thread.sleep();<cr>}<esc>hxA catch(Exception ex) {<cr> ex.printStackTrace();<cr>}<esc>hx3k$hi
 
</pre></code>
 
</pre></code>
 
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.
 
 
 
----
 
----

Revision as of 00:07, 29 November 2009

Tip 4 Printable Monobook Previous Next

created February 24, 2001 · complexity basic · author Jamo.sp · version 5.7


In insert mode, type the first couple of characters of a word, then press
Ctrl-N to insert the next matching word, or
Ctrl-P to insert the previous matching word.

This is particularly useful when entering the names of variables in a program.

The 'complete' option controls where the keywords are searched (include files, tag files, buffers, ...).
The 'completeopt' option controls how the completion occurs (for example, whether a menu is shown).

References

Comments

Also see script#182 and script#73


Completion such as this is also why it is a good idea to have descriptive variable names, so you can easily tell if you have the completion you were after.


It's completion that made me unable to use Visual Studio's editor (which has some completion, but it's not as good) anymore. I get lots of use out of:

File completion: <C-X><C-F>
Line completion: <C-X><C-L>
Omni completion: <C-X><C-O>


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<CR>lines

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. For example, 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 /*<cr><cr>/<Up>

which will expand to:

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

Here are some useful abbreviations for Java code:

abbr psvm public static void main(String[] args){<cr>}<esc>O
abbr sysout System.out.println("");<esc>2hi
abbr sop System.out.println("");<esc>2hi
abbr syserr System.err.println("");<esc>2hi
abbr sep System.err.println("");<esc>2hi

abbr forl for (int i = 0; i < ; i++) {<esc>7hi
abbr tryb try {<cr>} catch (Exception ex) {<cr> ex.printStackTrace();<cr>}<esc>hx3ko
abbr const public static final int

abbr ctm System.currentTimeMillis()
abbr slept try {<cr> Thread.sleep();<cr>}<esc>hxA catch(Exception ex) {<cr> ex.printStackTrace();<cr>}<esc>hx3k$hi