5

Some markdown as an example. I select the following paragraph in visual mode and press gq. The result is:

-   Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
    eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum.

But what I expect is:

-   Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
    eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
    ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
    aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
    in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
    Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
    officia deserunt mollit anim id est laborum.

Is this normal behavior? If yes, why and how could I change it? Thanks a lot!

1 Answer 1

3

You can fix this with the following command:

:set autoindent

This causes the indent of the second line also to be used for the rest of the list item.

Vim's included filetype plugin for Markdown formats bulleted lists using the 'comments' and 'formatoptions' settings.

With q included in the formatoptions setting, Vim will allow the formatting of "comments" using the gq command.

The 'comments' setting is used to defined what is considered a comment. The markdown plugin sets this to include fb:-, which means that a line that begins with a hyphen followed by whitespace is treated as a comment. The f means that:

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

But, as you have discovered, the autoindent setting — which is not set by Vim's standard markdown plugin — is required to carry this indentation over into subsequent lines.

Note that Vim also provides a more flexible mechanism for formatting lists: the 'formatlistpat' option. There is a nice article on this, which goes into a bit more detail than the documentation, in last year's edition of Vimways.

2
  • This works! Thank you!
    – cmosig
    Sep 9, 2019 at 9:01
  • @numberking No probs. See my edit for a bit more explanation of exactly what's going on.
    – Rich
    Sep 9, 2019 at 9:06

Your Answer

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

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