4. Auto-Completing Words

Consider the following code

Figure 9. Auto-completion example

The function A_Very_Long_Function_Name() can be quite exasperating to type over and over again. While still in insert-mode, one can auto-complete a word by either searching forwards or backwards. In function, Another_Function() one can type A_Very... and hit CTRL-P. The first matching word will be displayed first. In this case it would be A_Very_Long_Variable_Name. To complete it correctly, one can hit CTRL-P again and the search continues upwards to the next matching word, which is A_Very_Long_Function_Name. As soon as the correct word is matched you can continue typing. VIM remains in insert-mode during the entire process.

Similar to CTRL-P is the keystroke CTRL-N. This searches forwards instead of backwards. Both the keystrokes continue to search until they hit the top or bottom.

Both CTRL-P and CTRL-N are part of a mode known as CTRL-X mode. CTRL-X mode is a sub-mode of the insert mode. So you can enter this mode when you are in the insert-mode. To leave CTRL-X mode you can hit any keystroke other than CTRL-X, CTRL-P and CTRL-N. Once you leave CTRL-X mode you return to insert-mode.

CTRL-X mode allows you do auto-completion in a variety of ways. One can even autocomplete filenames. This is particularly useful when you have to include header files. Using CTRL-X mode you can include a file foo.h using the following mechanism.

	 #include "f CTRL-X CTRL-F"
      

That's CTRL-X CTRL-F. I know... I know... Its sounds like emacs ;-). There are other things you can do in the CTRL-X mode. One of them is dictionary completion. Dictionary completion allows one to specify a file containing a list of words which are used for completion. By default the dictionary option is not set. This option is set by the command :set dictionary=file. Typically one can put in C keywords, typedefs, #defines in the dictionary file. C++ and Java programmers may be interested in adding class names as well.

The format of a dictionary file is simple. Just put a word you want in line by itself. So a C dictionary file would look something like this.

Figure 10. A sample dictionary file

To use the dictionary completion, one needs to hit CTRL-X CTRL-K. The completion is similar to the CTRL-P and CTRL-N keystrokes. So... to type "typedef" all one needs to do is t CTRL-X CTRL-K and poof... the name completed.

Hosting by: Hurra Communications Ltd.
Generated: 2007-01-26 17:57:44