with indents
of 4 spaces), here is what you can use: >
:set shiftwidth=4
:set softtabstop=-1
:set expandtab
<
If you want the same behavior but with better control over alignment
(e.g. lining up parameters or comments in multiples of 2 spaces), use: >
:set shiftwidth=4
:set softtabstop=2
:set expandtab
:set smarttab
<
If instead, you would like to write C code like Bram Moolenaar would have
(using a mix of tabs and spaces), you can use >
:set shiftwidth=4
:set softtabstop=-1
<
==============================================================================
*30.6* Formatting comments
One of the great things about Vim is that it understands comments. You can
ask Vim to format a comment and it will do the right thing.
Suppose, for example, that you have the following comment: >c
/*
* This is a test
* of the text formatting.
*/
You then ask Vim to format it by positioning the cursor at the start of the
comment and type: >
gq]/
"gq" is the operator to format text. "]/" is the motion that takes you to the
end of a comment. The result is: >c
/*
* This is a test of the text formatting.
*/
Notice that Vim properly handled the beginning of each line.
An alternative is to select the text that is to be formatted in Visual mode
and type "gq".
To add a new line to the comment, position the cursor on the middle line and
press "o". The result looks like this: >c
/*
* This is a test of the text formatting.
*
*/
Vim has automatically inserted a star and a space for you. Now you can type
the comment text. When it gets longer than 'textwidth', Vim will break the
line. Again, the star is inserted automatically: >c
/*
* This is a test of the text formatting.
* Typing a lot of text here will make Vim
* break
*/
For this to work some flags must be present in 'formatoptions':
r insert the star when typing <Enter> in Insert mode
o insert the star when using "o" or "O" in Normal mode
c break comment text according to 'textwidth'
See |fo-table| for more flags.
DEFINING A COMMENT
The 'comments' option defines what a comment looks like. Vim distinguishes
between a single-line comment and a comment that has a different start, end
and middle part.
Many single-line comments start with a specific character. In C++ // is
used, in Makefiles #, in Vim scripts ". For example, to make Vim understand
C++ comments: >
:set comments=://
The colon separates the flags of an item from the text by which the comment is
recognized.