Vim, Markdown, and Unordered List Indentation

I enjoy writing in Vim and neovim, but I've been seeing this curious thing when writing unordered lists.

After writing a unordered line, when I press enter to go to the next line, it indents.

* This is a unordered line ^--- cursor starts here ^--- cursor position after pressing "enter"

When writing long unordered lines this is great! However, this isn't what I want when making many short unordered lists, such as when copying lists from paper notes.

My initial thought was that this might be related to autoindent, but it turns out to be related to something called formatoptions and how it changes handling of comments.

formatoptions changes how automatic formatting operates in Vim based on character flags which can be individually enabled or disabled.

Repurposed code comments

formatoptions works because Vim actually treats unordered lists like code comments. It has a feature where it will automatically continue comment styles on the next line for you, which is helpful to reformat code comments with gqq.

Where does it specify the comment types though?

set comments

The output isn't immediately clear, but we can look at help format-comments to decipher the output:

fb:*,fb:-,fb:+,n:>

This shows *, -, and + has having the initial comment string only on the first line ("f"), and must be followed by a blank ("b"). Blockquotes start with > and can be nested ("n").

formatoptions

help formatoptions redirects us to fo-table which describes what each setting does. There's two of particular interest in this case.

r Automatically insert the current comment leader after hitting <Enter> in Insert mode. o Automatically insert the current comment leader after hitting 'o' or 'O' in Normal mode. In case comment is unwanted in a specific place use CTRL-U to quickly delete it. |i_CTRL-U|

By removing the r and o settings, the cursor now goes back to the beginning of line, simplifying the creation of quick lists for me.

set formatoptions-=ro
* This is a unordered line ^--- cursor starts here ^--- cursor position after pressing "enter" is now at the start of the line

Disabling at startup

I don't usually use bullet points for long content, so I disable this when Vim sees a Markdown file type.

autocmd FileType markdown set formatoptions-=ro

Another blogger looked into why numbered lists didn't format in a desired way when using gqq, you might want to have a look. This author looks at formatlistpat and its effects.