listvimmarkdown

Markdown lists in Vim


When creating a list in a markdown file, I would like to insert a new item when pressing <CR>. I want that to apply to both ordered and unordered lists. That implies that, in ordered lists, the list item number will increment automatically. Also, if I press <CR> on a list item with no content, I would like to remove the item and add a new line, essentially ending the list; This is the current behaviour.

I managed to achieve some of the functionality that I want thanks to this StackOverflow question with this autocommand:

autocmd Filetype markdown setlocal com=s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,b:- | set formatoptions=tcroqln

But that autocommand doesn't work with ordered lists and doesn't end the list when <CR> is pressed on a blank list item.

Is it possible to add those two features?


Solution

  • To handle the ordered lists, you could treat them as comments like you do with the - sigil, just add :setlocal com+=b:1. This won't auto-increment, but I think Markdown does this for you when rendering the list, anyway.

    There's no built-in logic for ending a list, but you can simply press <C-U> to remove the automatically inserted comment. If that's not good enough for you, an :imap <buffer> <CR> <CR>... mapping can detect such situations (by a function to be invoked in the ... part) and then remove that automatically.