at the start of each line. The {flags} field may be
empty.
Any blank space in the text before and after the {string} is part of the
{string}, so do not include leading or trailing blanks unless the blanks are a
required part of the comment string.
When one comment leader is part of another, specify the part after the whole.
For example, to include both "-" and "->", use >
:set comments=f:->,f:-
A three-piece comment must always be given as start,middle,end, with no other
parts in between. An example of a three-piece comment is >
sr:/*,mb:*,ex:*/
for C-comments. To avoid recognizing "*ptr" as a comment, the middle string
includes the 'b' flag. For three-piece comments, Vim checks the text after
the start and middle strings for the end string. If Vim finds the end string,
the comment does not continue on the next line. Three-piece comments must
have a middle string because otherwise Vim can't recognize the middle lines.
Notice the use of the "x" flag in the above three-piece comment definition.
When you hit Return in a C-comment, Vim will insert the middle comment leader
for the new line: " * ". To close this comment you just have to type "/"
before typing anything else on the new line. This will replace the
middle-comment leader with the end-comment leader and apply any specified
alignment, leaving just `" */"`. There is no need to hit Backspace first.
When there is a match with a middle part, but there also is a matching end
part which is longer, the end part is used. This makes a C style comment work
without requiring the middle part to end with a space.
Here is an example of alignment flags at work to make a comment stand out
(kind of looks like a 1 too). Consider comment string: >vim
:set comments=sr:/***,m:**,ex-2:******/
>
/*** ~
**<--right aligned from "r" flag ~
** ~
offset 2 spaces for the "-2" flag-->** ~
******/ ~
In this case, the first comment was typed, then return was pressed 4 times,
then "/" was pressed to end the comment.
Here are some finer points of three part comments. There are three times when
alignment and offset flags are taken into consideration: opening a new line
after a start-comment, opening a new line before an end-comment, and
automatically ending a three-piece comment. The end alignment flag has a
backwards perspective; the result is that the same alignment flag used with
"s" and "e" will result in the same indent for the starting and ending pieces.
Only one alignment per comment part is meant to be used, but an offset number
will override the "r" and "l" flag.
Enabling 'cindent' will override the alignment flags in many cases.
Reindenting using a different method like |gq| or |=| will not consult
alignment flags either. The same behaviour can be defined in those other
formatting options. One consideration is that 'cindent' has additional options
for context based indenting of comments but cannot replicate many three piece
indent alignments. However, 'indentexpr' has the ability to work better with
three piece comments.
Other examples: >
"b:*" Includes lines starting with "*", but not if the "*" is
followed by a non-blank. This avoids a pointer dereference
like "*str" to be recognized as a comment.
"n:>" Includes a line starting with ">", ">>", ">>>", etc.
"fb:-" Format a list that starts with "- ".
By default, "b:#" is included. This means that a line that starts with
"#include" is not recognized as a comment line. But a line that starts with
"# define" is recognized. This is a compromise.
*fo-table*
You can use the 'formatoptions' option to influence how Vim formats text.
'formatoptions' is a string that can contain any of the letters below. You
can separate the option letters with commas for readability.
letter meaning when present in 'formatoptions' ~
*fo-t*
t Auto-wrap text using 'textwidth'