4

When using plain text I write bullet lists like this (second and further lines indented with two spaces):

* blah blah blah
  blah blah

* yadda yadda yadda

Problem is, when I start out a new bullet item:

* blah blah blah

and then press Enter, the second line isn't automatically indented like I want it to be:

* blah blah blah
blah

Since I have autoindent set, once I correct the indentation of the second line the third and subsequent lines indent correctly. Also, if I use gq to format the bullet list item it will format it the way I want. But I want vim to automatically handle it.

Also, I want the indentation mechanism to indent two spaces further if I hit Enter when the first non-whitespace character on the line is a *, to deal with nested bullet items. For instance, if I have:

* blah blah
  * yadda yadda

When I hit Enter at the end of the "yadda"s I want the indentation to be at 4 spaces instead of 2, so when I type more "yadda"s it comes out like this:

* blah blah
  * yadda yadda
    yadda yadda

formatexpr is unset and formatoptions is tcq

0

1 Answer 1

4

You can get it to do so by adding r to your 'formatoptions' and amending your 'comments' setting with the following commands:

set formatoptions+=r
set comments-=mb:*
set comments+=fb:*

(If you don't need to keep the rest of your 'comments' setting, you could of course just use the single command: set comments=fb:*)

Note that this alters the * entry in your comments to match the existing - entry, which already indents in the manner you describe.

Some excerpts from :help comments explain how this works:

Vim can automatically insert and format comments in a special way. Vim recognizes a comment by a specific string at the start of the line (ignoring white space). Three types of comments can be used:

  • A comment string that repeats at the start of each line. An example is the type of comment used in shell scripts, starting with "#".
  • A comment string that occurs only in the first line, not in the following lines. An example is this list with dashes.

[...]

The 'comments' option is a comma-separated list of parts. Each part defines a type of comment string. A part consists of: {flags}:{string}

{flags}:

[...]

b Blank (, or ) required after {string}.

f Only the first line has the comment string. Do not repeat comment on the next line, but preserve indentation (e.g., a bullet-list).

(emphasis mine)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.