old-www/HOWTO/C-editing-with-VIM-HOWTO/auto-complete.html

276 lines
4.8 KiB
HTML

<HTML
><HEAD
><TITLE
> Auto-Completing Words
</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK
REL="HOME"
TITLE=" C editing with VIM HOWTO
"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE=" Jumping to random positions in C files
"
HREF="random.html"><LINK
REL="NEXT"
TITLE=" Formating automatically
"
HREF="auto-format.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>C editing with VIM HOWTO</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="random.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="auto-format.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AUTO-COMPLETE"
>4. Auto-Completing Words</A
></H1
><P
> Consider the following code
</P
><DIV
CLASS="FIGURE"
><A
NAME="AEN224"
></A
><P
><B
>Figure 9. Auto-completion example
</B
></P
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="auto.png"
></IMG
></P
></DIV
></DIV
><P
> 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 <B
CLASS="KEYCAP"
>CTRL-P</B
>. 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 <B
CLASS="KEYCAP"
>CTRL-P</B
> 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.
</P
><P
> Similar to <B
CLASS="KEYCAP"
>CTRL-P</B
> is the keystroke
<B
CLASS="KEYCAP"
>CTRL-N</B
>. This searches forwards instead of
backwards. Both the keystrokes continue to search until they
hit the top or bottom.
</P
><P
> Both <B
CLASS="KEYCAP"
>CTRL-P</B
> and <B
CLASS="KEYCAP"
>CTRL-N</B
> 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.
</P
><P
> 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.
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> #include "f CTRL-X CTRL-F"
</PRE
></TD
></TR
></TABLE
><P
> 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 <B
CLASS="COMMAND"
>:set dictionary=file</B
>. 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.
</P
><P
> 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.
</P
><DIV
CLASS="FIGURE"
><A
NAME="AEN245"
></A
><P
><B
>Figure 10. A sample dictionary file
</B
></P
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="dict.png"
></IMG
></P
></DIV
></DIV
><P
> To use the dictionary completion, one needs to hit
<B
CLASS="KEYCAP"
>CTRL-X CTRL-K</B
>. The completion is similar to
the <B
CLASS="KEYCAP"
>CTRL-P</B
> and <B
CLASS="KEYCAP"
>CTRL-N</B
>
keystrokes. So... to type "typedef" all one needs to do is
t CTRL-X CTRL-K and poof... the name completed.
</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="random.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="auto-format.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Jumping to random positions in C files</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Formating automatically</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>