I just realized that VIM 7.3 has built-in support for highlighting Markdown files. Excellent. However, it doesn't fold on the headings.
Can any offer suggestions on how to get this working?
Alternatively, I'm using Markdown only as a way to get simple structured text. If there is a better alternative format, please also suggest. But not sure I dig TVO or VimOutliner.
I had the same question, and played around with Jander's nice solution. The only problem is that by defining folding using syntax, you lose any Markdown syntax highlighting.
Given that you might be interested in alternate markups, I would suggest using reStructuredText, and the amazing Vst vim extension. It does folding very nicely. Rst is much more powerful than Markdown.
You can enable markdown folding feature by adding this in your
.vimrc
if you are using the latest version of Vim - no need to be the latest, but I don't know the exact version.For some reason it's not documented in the README but you can find the related code in the repository.
FYI, if you don't want the sections closed when you open a file, refer to this SO thread. I think adding this would be the best way but you may have a different preference.
As of Vim 8 it is included by default (via Tim Pope's markdown plugin). Just add this to .vimrc:
To make sure you have this plugin loaded you can run
and look for
When I use markdown I only use the hash-style headings with space separating hashes and text. This makes the folding task a lot simpler.
I'm pretty new to Vim, so use the following at your own risk. I added the following code to my vimrc and it folds headings based on number of hashes, and it retains the syntax colouring.
There is a vim-markdown plugin at https://github.com/plasticboy/vim-markdown .
The code related to folding from there appears to be:
Here is a try at a recursive header folding rule. It doesn't include the underline style of Markdown header, but I'm guessing those would be awkward for your purposes anyway.
Put the following code into your .vimrc: