1

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 fo+=tacqw.

2 Answers 2

4

The 'mail' ft plugin (at least in vim 7.4) does this by making '>' a comment leader, like this:

set comments+=n:>

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

set comments+=b:>

or, for just plain, do

set comments+=:>

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:

set fo+=qcro

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.

8
  • Thanks, this does create the right behavior for vim in general. But for some reason it doesn't work in my markdown files. They end with .md and vim seem to recognize them as markdown because there is syntax highlighting. I don't have any extra plugins, just the latest version of vim. Can vim treat different file types differently other than giving them the right syntax highlighting?
    – user.S
    Aug 9, 2017 at 11:22
  • It does do more than syntax highlighting. Looking at vim7.4, it loads the ftplugin/markdown.vim file in the runtime path, which does set the block quote comment leader, but also unsets "r" and "o" from format options. Adding those back made things better for me. Could you provide the output of :set fo?, :set comments? and the first 3 or 4 lines of :version for me? Those should help me track down what is going on.
    – John O'M.
    Aug 9, 2017 at 12:11
  • I get formatoptions=awtcqln (so r and o is removed even though I added them in .vimrc), comments=fb:*,fb:-,fb:+,n:> (even though i put set comments+=b:> in .vimrc) and version VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Aug 3 2017 18:29:25) Included patches: 1-851 Compiled by Arch Linux
    – user.S
    Aug 9, 2017 at 12:30
  • If I simply start a line with '>' and keep typing until I get a new line, it works as it should. I can't reformat with gq though. Even if I can't add r to my formatoptions in .vimrc, if I add it when running vim it works – I get a new '>' when pressing enter. I can live without the latter but it would be really useful to be able to reformat a big chunk of text that I've pasted.
    – user.S
    Aug 9, 2017 at 19:09
  • I think what is happening is that your vimrc settings are applying, and then the filetype plugin for markdown is running afterwards and changing the settings. Fortunately, autocommands may help. Try adding autocmd BufRead,BufNew *.md set fo+=ro to your vimrc. It should set formatoptions the way we want when opening an existing or new .md file. It seems to work for me. If it works for you too, I'll update my answer.
    – John O'M.
    Aug 11, 2017 at 5:35
1

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 r and 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]J where [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.

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.