This commit is contained in:
gferg 2001-01-17 14:43:24 +00:00
parent d3291f3a7d
commit 90f06738a8
1 changed files with 118 additions and 61 deletions

View File

@ -2,6 +2,8 @@
<Article id="index"> <Article id="index">
<!-- Article Header -->
<ArtHeader> <ArtHeader>
<Title> <Title>
@ -30,15 +32,55 @@
</Author> </Author>
<PubDate> <revhistory>
v0.1, Dec 04 2000
</PubDate> <revision>
<revnumber>
v1.0
</revnumber>
<date>
Jan 14, 2001
</date>
<authorinitials>
sh
</authorinitials>
<revremark>
Second Revision. Corrected some typos.
</revremark>
</revision>
<revision>
<revnumber>
v0.1
</revnumber>
<date>
Dec 04, 2000
</date>
<authorinitials>
sh
</authorinitials>
<revremark>
First Revision. I would love to have your feedback
</revremark>
</revision>
</revhistory>
<abstract> <abstract>
<para> <para>
This document gives an introduction to editing C and This document gives an introduction to editing C and
other language files whose syntax is similar like C++ and other language files, whose syntax is similar, like C++ and
Java in vi/VIM. Java in vi/VIM.
</para> </para>
@ -46,6 +88,8 @@
</ArtHeader> </ArtHeader>
<!-- Section 1: Introduction -->
<sect1 id="intro"> <sect1 id="intro">
<title> <title>
Introduction Introduction
@ -69,6 +113,8 @@
</sect1> </sect1>
<!-- Section 2: Moving Around -->
<sect1 id="moving"> <sect1 id="moving">
<title> <title>
Moving around. Moving around.
@ -133,7 +179,7 @@
</para> </para>
<para> <para>
<keycap>e</keycap> is similar to <keycap> w</keycap> only <keycap>e</keycap> is similar to <keycap>w</keycap> only
that it takes you to the end of the current word and not that it takes you to the end of the current word and not
to the beginning of the next word. to the beginning of the next word.
</para> </para>
@ -199,7 +245,7 @@
<para> <para>
The above snippet shows two paragraphs. One can easily The above snippet shows two paragraphs. One can easily
move from the beginning of one to the other by using the move from the beginning of one to the other, by using the
<keycap>{</keycap> and <keycap>}</keycap> <keycap>{</keycap> and <keycap>}</keycap>
keys. <keycap>{</keycap> will take the cursor to the keys. <keycap>{</keycap> will take the cursor to the
paragraph above and <keycap>}</keycap> will take the paragraph above and <keycap>}</keycap> will take the
@ -207,9 +253,9 @@
</para> </para>
<para> <para>
Many people have the coding style where logical set of Many people have the coding style where a logical set of
statements are grouped together and separated from sets by statements are grouped together and separated by
a blank line. one or more blank lines.
</para> </para>
<para> <para>
@ -317,7 +363,7 @@
take you to the opening { of the bar() function. The take you to the opening { of the bar() function. The
reverse is slightly different. If you were in the middle of bar() reverse is slightly different. If you were in the middle of bar()
and you type <keycap>[[</keycap> the cursor will move to and you type <keycap>[[</keycap> the cursor will move to
the first { above ie the beginning of bar() itself. One the first { above i.e. the beginning of bar() itself. One
has to type <keycap>[[</keycap> again to move to the has to type <keycap>[[</keycap> again to move to the
beginning of foo(). The number of keystrokes can be beginning of foo(). The number of keystrokes can be
minimized by typing <keycap>2[[</keycap> to take the minimized by typing <keycap>2[[</keycap> to take the
@ -395,7 +441,7 @@
the cursor will jump to the corresponding match. the cursor will jump to the corresponding match.
</para> </para>
<para> Amongst other things the <keycap>%</keycap> keystroke <para> Amongst other things, the <keycap>%</keycap> keystroke
can be used to match #if, #ifdef, #else #elif and #endif can be used to match #if, #ifdef, #else #elif and #endif
also. also.
</para> </para>
@ -434,14 +480,12 @@
</figure> </figure>
<para> <para>
Checking the above code will involve the correctness of Checking the above code will involve checking the correctness
the parenthesis. The <keycap>%</keycap> can be used to of the parenthesis. The <keycap>%</keycap> can be used to
jump from one ( to it corresponding ) and vice versa. jump from one ( to its corresponding ) and vice versa.
Thus one can find which parenthesis corresponds to which Thus, one can find which opening parenthesis corresponds to
other closing parenthesis and use the information to which closing parenthesis and use the information to
validate the code. validate the code.
</para> </para>
@ -454,6 +498,8 @@
</sect1> </sect1>
<!-- Section 3: Jumping to Random positions -->
<sect1 id="Random"> <sect1 id="Random">
<title> <title>
Jumping to random positions in C files Jumping to random positions in C files
@ -465,11 +511,11 @@
</title> </title>
<para> <para>
A Tag are a sort of placeholder. Tags are very useful in A Tag is a sort of placeholder. Tags are very useful in
understanding and editing C files. Tags are a set of understanding and editing C. Tags are a set of
book-marks to each function in the C file. Tags are very book-marks to each function in a C file. Tags are very
useful in jumping to the definition of a function from useful in jumping to the definition of a function from
where it is called and back. where it is called and then jumping back.
</para> </para>
<para> <para>
@ -518,23 +564,23 @@
<para> <para>
Lets say that you are editing the function foo() and you Lets say that you are editing the function foo() and you
come across the function bar(). Now, to see what bar() come across the function bar(). Now, to see what bar()
does, one makes uses of tags. One can jump to the does, one makes uses of Tags. One can jump to the
definition of bar() and then jump back later. If need be definition of bar() and then jump back later. If need be,
one can jump to another function called within bar() and back. one can jump to another function called within bar() and back.
</para> </para>
<para> <para>
To use tags one must first run the program ctags on all To use Tags one must first run the program ctags on all
the source files. This creates a file called tags. This the source files. This creates a file called tags. This
file contains pointers to all the function definitions and file contains pointers to all the function definitions and
is used by VIM to take you to the function definition. is used by VIM to take you to the function definition.
</para> </para>
<para> <para>
The actual keystrokes for jumping to and back are The actual keystrokes for jumping to and fro are
<keycap>CTRL-]</keycap> and <keycap>CTRL-T</keycap>. By <keycap>CTRL-]</keycap> and <keycap>CTRL-T</keycap>. By
hitting <keycap>CTRL-]</keycap> in foo() at the place hitting <keycap>CTRL-]</keycap> in foo() at the place
where bar() is called takes the cursor to the beginning of where bar() is called, takes the cursor to the beginning of
bar(). One can jump back from bar() to foo() by just bar(). One can jump back from bar() to foo() by just
hitting <keycap>CTRL-T</keycap>. hitting <keycap>CTRL-T</keycap>.
</para> </para>
@ -558,9 +604,9 @@
<para> <para>
In case of a source tree which contains C files in In case of a source tree which contains C files in
different sub directories one can call ctags in the root different sub directories, one can call ctags in the root
directory of the source tree with the -R option and a tags directory of the source tree with the -R option and a tags
file containing tags to all functions in the source tree file containing Tags to all functions in the source tree
will be created. For Example. will be created. For Example.
</para> </para>
@ -581,16 +627,16 @@
</title> </title>
<para> <para>
Marks are place-holders like tags. However, marks can be Marks are place-holders like Tags. However, marks can be
set at any point in a file and is not limited to only set at any point in a file and is not limited to only
functions, enums etc.. Plus marks have be set manually by functions, enums etc.. Plus marks have be set manually by
the user himself. the user.
</para> </para>
<para> <para>
By setting a mark there is no visible indication of the By setting a mark there is no visible indication of the
same. A mark is just a position in a file which is same. A mark is just a position in a file which is
remembered. Consider the following code remembered by VIM. Consider the following code
</para> </para>
<figure> <figure>
@ -635,14 +681,14 @@
<para> <para>
Suppose you are editing the line x++; and you want to come Suppose you are editing the line x++; and you want to come
back to that line after editing some other line. One can back to that line after editing some other line. You can
set a mark on that line with the keystroke set a mark on that line with the keystroke
<keycap>m'</keycap> and come back to the same line later <keycap>m'</keycap> and come back to the same line later
by hitting <keycap>''</keycap>. by hitting <keycap>''</keycap>.
</para> </para>
<para> <para>
VIM allows you to set multiple marks. These marks are VIM allows you to set more than one mark. These marks are
stored in registers a-z, A-Z and 1-0. To set a mark and stored in registers a-z, A-Z and 1-0. To set a mark and
store the same in a register say j, all one has to hit is store the same in a register say j, all one has to hit is
<keycap>mj</keycap>. To go back to the mark one has to hit <keycap>mj</keycap>. To go back to the mark one has to hit
@ -658,14 +704,14 @@
<para> <para>
Marks can span across files. To use such marks one has to Marks can span across files. To use such marks one has to
use upper-case registers ie A-Z. Lower-case registers are use upper-case registers i.e. A-Z. Lower-case registers are
used only within files and do not span files. That's to used only within files and do not span files. That's to
say, if you were to set a mark in a file foo.c in register say, if you were to set a mark in a file foo.c in register
"a" and then move to another file and hit "a" and then move to another file and hit
<keycap>'a</keycap>, there will not change to the cursor <keycap>'a</keycap>,
location. The cursor will not jump back to the previous the cursor will not jump back to the previous
location. If you want a mark which will take you to a location. If you want a mark which will take you to a
different file then you will need to use upper-case different file then you will need to use an upper-case
register. For example, use <keycap>mA</keycap> instead of register. For example, use <keycap>mA</keycap> instead of
<keycap>ma</keycap>. I'll talk about editing multiple <keycap>ma</keycap>. I'll talk about editing multiple
files in a later section. files in a later section.
@ -745,6 +791,8 @@
</sect1> </sect1>
<!-- Section 4: Auto-completing words -->
<sect1 id="Auto-Complete"> <sect1 id="Auto-Complete">
<Title> <Title>
Auto-Completing Words Auto-Completing Words
@ -795,16 +843,16 @@
</figure> </figure>
<para> <para>
The function AVeryLongFunctionName() can be quite The function A_Very_Long_Function_Name() can be quite
exasparating to type over and over again. While in exasperating to type over and over again. While still in
insert-mode, one can auto-complete a word by either searching insert-mode, one can auto-complete a word by either searching
forwards or backwards. In function, AnotherFunction() one can forwards or backwards. In function, Another_Function() one can
type AVery... and hit <keycap>CTRL-P</keycap>. The first type A_Very... and hit <keycap>CTRL-P</keycap>. The first
matching word will be displayed first. In this case it would matching word will be displayed first. In this case it would
be AVeryLongVariableName. To complete it correctly, one can be A_Very_Long_Variable_Name. To complete it correctly, one can
hit <keycap>CTRL-P</keycap> again and the search continues hit <keycap>CTRL-P</keycap> again and the search continues
upwards to the next matching word, which is upwards to the next matching word, which is
AVeryLongFunctionName. As soon as the correct word is matched A_Very_Long_Function_Name. As soon as the correct word is matched
you can continue typing. VIM remains in insert-mode during you can continue typing. VIM remains in insert-mode during
the entire process. the entire process.
</para> </para>
@ -826,7 +874,7 @@
</para> </para>
<para> <para>
CTRL-X mode allows you do autocompletion in a variety of CTRL-X mode allows you do auto-completion in a variety of
ways. One can even autocomplete filenames. This is ways. One can even autocomplete filenames. This is
particularly useful when you have to include header particularly useful when you have to include header
files. Using CTRL-X mode you can include a file foo.h using files. Using CTRL-X mode you can include a file foo.h using
@ -834,7 +882,7 @@
</para> </para>
<programlisting> <programlisting>
#include "f^X^F" #include "f CTRL-X CTRL-F"
</programlisting> </programlisting>
<para> <para>
@ -913,11 +961,13 @@
<keycap>CTRL-X CTRL-K</keycap>. The completion is similar to <keycap>CTRL-X CTRL-K</keycap>. The completion is similar to
the <keycap>CTRL-P</keycap> and <keycap>CTRL-N</keycap> the <keycap>CTRL-P</keycap> and <keycap>CTRL-N</keycap>
keystrokes. So... to type "typedef" all one needs to do is keystrokes. So... to type "typedef" all one needs to do is
t^X^K and voila, the name completed. t CTRL-X CTRL-K and poof... the name completed.
</para> </para>
</sect1> </sect1>
<!-- Section 5: Auto Formating -->
<sect1 id="auto-format"> <sect1 id="auto-format">
<title> <title>
Formating automatically Formating automatically
@ -966,9 +1016,8 @@
<para> <para>
While coding in C, one often indents inner-blocks of While coding in C, one often indents inner-blocks of
code. To do this automatically while coding, VIM has code. To do this automatically while coding, VIM has an
command called cindent. To set this, the following command option called cindent. To set this, just use the command
is used
</para> </para>
<screen> <screen>
@ -977,7 +1026,7 @@
<para> <para>
By setting cindent, code is automatically beautified. To By setting cindent, code is automatically beautified. To
set this command automatically, just set it in your .vimrc set this command automatically, just add it to your .vimrc
</para> </para>
</sect2> </sect2>
@ -1060,6 +1109,8 @@
</sect1> </sect1>
<!-- Section 6: Editing Multiple files -->
<sect1 id="multi"> <sect1 id="multi">
<title> <title>
Multi-file editing Multi-file editing
@ -1068,7 +1119,7 @@
<para> <para>
One often needs to edit more than one file at a time. For One often needs to edit more than one file at a time. For
example one maybe editing a header file and a source file at example one maybe editing a header file and a source file at
the same time. To editing more than one file at a time, the same time. To edit more than one file at a time,
invoke VIM using the following command invoke VIM using the following command
</para> </para>
@ -1117,14 +1168,16 @@
<para> <para>
After executing that command, you'll find that there are two After executing that command, you'll find that there are two
files visible. One with the first file and the other with the files visible. One window shows the first file and the other shows
new file. To switch between the files on has to use the the second file. To switch between the files one has to use the
keystroke <keycap>CTRL-W CTRL-W</keycap>. To learn more about keystroke <keycap>CTRL-W CTRL-W</keycap>. To learn more about
split windows, just run help on it. split windows, just run help on it.
</para> </para>
</sect1> </sect1>
<!-- Section 7: Quickfix -->
<sect1 id="quickfix"> <sect1 id="quickfix">
<title> <title>
Quickfix Quickfix
@ -1136,7 +1189,7 @@
I've mentioned earlier, save the file, compile the code and I've mentioned earlier, save the file, compile the code and
go to the error(s) and start editing again. VIM helps save go to the error(s) and start editing again. VIM helps save
the cycle time slightly using a mode called the cycle time slightly using a mode called
quickfix. Basically, one saves the compiler errors in a file quickfix. Basically, one has to save the compiler errors in a file
and open the file with VIM using the command and open the file with VIM using the command
</para> </para>
@ -1150,10 +1203,10 @@
</para> </para>
<para> <para>
There is a shortcut to the cycle. Using the make command, one There is a shortcut to the cycle. Using the command "make", one
can automatically compile code and goto the position where can automatically compile code and goto the position where
the first error occurs. To invoke the make command use the the first error occurs. To invoke the make command type the
following command following
</para> </para>
<screen> <screen>
@ -1258,7 +1311,7 @@
<textobject> <textobject>
<programlisting> <programlisting>
:!gcc test.c 2>&1| tee /tmp/vim9821.err :!gcc test.c 2&gt;&amp;1| tee /tmp/vim9821.err
test.c: In function `main': test.c: In function `main':
test.c:6: parse error before `}' test.c:6: parse error before `}'
test.c:4: warning: return type of `main' is not `int' test.c:4: warning: return type of `main' is not `int'
@ -1312,7 +1365,7 @@
<textobject> <textobject>
<programlisting> <programlisting>
:!gcc test.c 2>&1| tee /tmp/vim9822.err :!gcc test.c 2&gt;&amp;1| tee /tmp/vim9822.err
Press RETURN or enter command to continue Press RETURN or enter command to continue
</programlisting> </programlisting>
@ -1333,13 +1386,15 @@
</sect1> </sect1>
<!-- Section 8: Copyright -->
<sect1 id="copying"> <sect1 id="copying">
<title> <title>
Copyright Copyright
</title> </title>
<para> <para>
Copyright (c) 2000 Siddharth Heroor. Copyright (c) 2000,2001 Siddharth Heroor.
</para> </para>
<para> <para>
@ -1356,6 +1411,8 @@
</sect1> </sect1>
<!-- Section 9: References -->
<sect1 id="references"> <sect1 id="references">
<title> <title>
References References