I want to use the combination gggqG to go to the top of the file and format the whole file from there, so that it fits into my 80 col textwidth border.

But when I do, vim ignores my markdown headers. So I want basically to ignore all markdown headers for formatting. How can I do that?


#this is a h1
this is the text of the file i want 
to reformat


#this is a h1 this
is the text of the file
i want to reformat

The word this was moved to the header.

  • what is your formatoptions setting? – Christian Brabandt Jan 2 '16 at 21:31
  • @ChristianBrabandt formatoptions=t,a – toogley Jan 2 '16 at 21:33

Since you don't have wrapping for comments enabled, the workaround in this Stack Overflow post can be used:

set comments+=n:#
set fo+=q

This adds # as a comment marker (allowing nesting, so that ##, ###, etc. also count).

I'd suggest leaving a space after # and using nb:# - some Markdown parsers require a space after #. The CommonMark spec also supports this.


If you're willing to change the way you write your Markdown slightly, you could fix this simply by introducing an extra blank line between your headings and the following text:

# this is a h1

this is the text of the file i want
to reformat

This is how I personally format my atx-style Markdown headers, anyway: I think it looks better and is easier to read.

  • Seconded, this is how I write too. Many things in Markdown benefit from blank lines around them. – muru Jan 7 '16 at 8:37
  • After posting my question i've noticed this too, but @muru 's stackoverflow link more helpful in my "general problem", so thanks I'll upvoted your post. – toogley Jan 7 '16 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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