program to an error
file. How this works depends on various things, such as the 'shell'. If your
":make" command doesn't capture the output, check the 'makeef' and
'shellpipe' options. The 'shellquote' and 'shellxquote' options might also
matter.
In case you can't get ":make" to redirect the file for you, an alternative is
to compile the program in another window and redirect the output into a file.
Then have Vim read this file with: >
:cfile {filename}
Jumping to errors will work like with the ":make" command.
==============================================================================
*30.2* Indenting C style text
A program is much easier to understand when the lines have been properly
indented. Vim offers various ways to make this less work. For C or C style
programs like Java or C++, set the 'cindent' option. Vim knows a lot about C
programs and will try very hard to automatically set the indent for you. Set
the 'shiftwidth' option to the amount of spaces you want for a deeper level.
Four spaces will work fine. One ":set" command will do it: >
:set cindent shiftwidth=4
With this option enabled, when you type something such as "if (x)", the next
line will automatically be indented an additional level.
if (flag)
Automatic indent ---> do_the_work();
Automatic unindent <-- if (other_flag) {
Automatic indent ---> do_file();
keep indent do_some_more();
Automatic unindent <-- }
When you type something in curly braces ({}), the text will be indented at the
start and unindented at the end. The unindenting will happen after typing the
'}', since Vim can't guess what you are going to type.
One side effect of automatic indentation is that it helps you catch errors in
your code early. When you type a } to finish a function, only to find that
the automatic indentation gives it more indent than what you expected, there
is probably a } missing. Use the "%" command to find out which { matches the
} you typed.
A missing ) and ; also cause extra indent. Thus if you get more white
space than you would expect, check the preceding lines.
When you have code that is badly formatted, or you inserted and deleted lines,
you need to re-indent the lines. The "=" operator does this. The simplest
form is: >
==
This indents the current line. Like with all operators, there are three ways
to use it. In Visual mode "=" indents the selected lines. A useful text
object is "a{". This selects the current {} block. Thus, to re-indent the
code block the cursor is in: >
=a{
If you have really badly indented code, you can re-indent the whole file with:
>
gg=G
However, don't do this in files that have been carefully indented manually.
The automatic indenting does a good job, but in some situations you might want
to overrule it.
SETTING INDENT STYLE
Different people have different styles of indentation. By default Vim does a
pretty good job of indenting in a way that 90% of programmers do. There are
different styles, however; so if you want to, you can customize the
indentation style with the 'cinoptions' option.
By default 'cinoptions' is empty and Vim uses the default style. You can
add various items where you want something different. For example, to make
curly braces be placed like this:
if (flag) ~
{ ~
i = 8; ~
j = 0; ~
} ~
Use this command: >
:set cinoptions+={2
There are many of these items. See |cinoptions-values|.
==============================================================================
*30.3* Automatic indenting
You don't want to switch on the 'cindent' option manually every time you edit
a C file. This is how you make it work automatically: >
:filetype indent on
Actually, this does a lot more than switching on 'cindent' for C files. First
of all, it enables detecting the type of a file. That's the same as what is
used for syntax highlighting.
When the filetype is known, Vim will search for an indent file for this
type of file. The Vim