- 论坛徽章:
- 0
|
11.10.2 C and C++ Programming Features
vim, in the grand tradition of vi, is first and foremost a programmer's editor. In
particular, it is a C programmer's editor, and happily, C++ programmers can take
advantage of it too. There are lots of features that make the C programmer's life
easier. We describe the most significant ones here.
11.10.2.1 Smart indenting
All versions of vi have the autoindent option, which, when set, automatically
indents the current line by the same amount as the one next to it. This is handy
for C programmers who indent their code, and for anyone else who may need to
indicate some kind of structure in their text via indentation.
vim carries this feature further, with two options, smartindent and cindent. The
cindent option is the more interesting of the two, and is the topic of this
subsection. See Table 11.14 for a list of vim indentation and formatting options.
Table 11.14. vim Indentation and Formatting Options
Option Function
autoindent Simple-minded indentation, uses that of the previous line.
smartindent Similar to autoindent, but knows a little about C syntax.
Deprecated in favor of cindent.
cindent Enables automatic indenting for C programs, and is quite smart.
C formatting is affected by the rest of the options in this table.
cinkeys Input keys that trigger indentation options.
cinoptions Allows you to tailor your preferred indentation style.
cinwords Keywords that start an extra indentation on the following line.
formatoptions
Made up of a number of single letter flags that control several
behaviors, notably how comments are formatted as you type
them.
comments
Describes different formatting options for different kinds of
comments, both those with starting and ending delimiters, as in
C, and those that start with a single symbol and go to the end of
the line, such as in a Makefile or shell program.
When set up appropriately, vim automatically rearranges the indentation of your
C program as you type. For instance, after an if, vim automatically indents the
next line. If the body of the if is enclosed in braces, when you type the right
brace, vim will automatically indent it back one tab stop, to line up underneath
the if. As another example, with the settings shown below, upon typing the
colon that goes with a case, vim will shift the line with the case left one tab stop
to line up under the switch.
The following .vimrc produces, in our opinion, very nicely formatted C code:
set nocp incsearchset cinoptions=:0,p0,t0set cinwords=if,else,while,do,for,switch,caseset formatoptions=tcqrset cindent |
|