When writing block quotes Markdown, I want to make vim act the same way it does with comment leaders and automatically start new lines with the '>'. I also want to be able to format text this way with
gq. How can I do this? I set my formatoptions to
The 'mail' ft plugin (at least in vim 7.4) does this by making '>' a comment leader, like this:
The "n:" means that they can be nested, as they often are in email. You probably don't need that, but might want some of the other options, like 'b' to require a space after the leader
or, for just plain, do
Then, having 'q' in your formatoptions (like you already have) should flow with the block quote leader. Likewise, the 'c' in your formatoptions (as you also already have) will keep automatically placing the leader as you are typing text when the line wraps.
I'd also recommend adding 'r' and 'o' to your format options, so that creating new lines (enter in insert mode or 'o' or 'O' in normal mode) also automatically adds the leader:
I expect those settings should work fine with markdown as well.
All of the flags for comment leaders are described at
:help format-comments which may help you customize your settings further.
It turns out vim handles block quotes in Markdown pretty much the way I asked for since at least version 7.4. It loads ftplugin/markdown.vim which sets '>' as a comment leader. It also adds the
t flag to
formatoptions and removes the
o flags. That means if you write until the text is longer than
textwidth, you get a new line that starts with
>. But if you just press enter or add a new line with o or O you won't get the '>' on the new line. So if you want this functionality you have to override the file type plugin. You can do that by creating the file
.vim/after/ftplugin/markdown.vim and adding
set formatoptions+=ro to it.
My situation was that I had some paragraphs in a Markdown file that I wanted to turn into blockquotes by adding '>' at the start of every line. The paragraphs were already formatted so there were hard line breaks in them. I added '>' to the start of the first line and expected vim to make the whole paragraph commented if I used
gq over it. Obviously that's not what
gq does when you have a hard line break after what it recognizes as a comment. If it did, code written after a comment when programming would itself be commented out.
We can solve the problem if we join the lines of the paragraph by pressing
[count] is the number of lines to be joined. Then
gq will format the paragraph as we want. If you have the
a flag in
formatoptions it's not necessary to use
gq because the text is automatically reformatted.