VIM has built-in feature for indenting code and
formatting comments. That can make coding a lot
easier.
To enable automatic indenting and formatting of C
code (or C-like code as in Java), put the following
lines in the file .vimrc in your home
directory. If the file does not exist, create
it.
set formatoptions=tcqro2
set autoindent
set smartindent
set cindent
Now restart VIM by typing the command vi on the
command prompt. Try to enter a program as
follows.
int main(void)
{
int i, j;
i = 10;
j = 15;
if(i < j)
{
i
+= 10;
}
else
j
= 30;
}
Notice that when you type if(...)
and press 'enter', VIM indents the next line
automatically. If you type { , then VIM
will go back and put it under i of
if . When you press enter again, VIM
will indent the next line.
Simliarly when you need to close a brace, just
type 'enter' to go to a new line and type
} , VIM will automatically align it to
its matching indent level.
VIM also formats comments. Just type
/* on a line and press enter. VIM will
put a * at start of each line and will
continue doing so until you type a / on
a fresh line.
This results in following format of comments
/*
* This comment is written using VIM.
* It is fun writing comments with VIM.
* You can relax and let VIM format everything
for you.
* Thats all I have to say now.
*/
For more information, consult VIM help.
|