Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim Markdown Folding?

Tags:

vim

markdown

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.

like image 605
Muchin Avatar asked Sep 30 '10 07:09

Muchin


People also ask

How do you toggle fold in Vim?

With the following in your vimrc, you can toggle folds open/closed by pressing F9. In addition, if you have :set foldmethod=manual , you can visually select some lines, then press F9 to create a fold. Here is an alternative procedure: In normal mode, press Space to toggle the current fold open/closed.

How do I fold text in Vim?

If you enter visual mode using v or V , then select a few lines of text using the movement keys, and type zf , Vim will create a fold comprising those lines.

What is code folding in Vim?

Code or text folding, or less commonly holophrasting, is a feature of some graphical user interfaces that allows the user to selectively hide ("fold") or display ("unfold") parts of a document. This allows the user to manage large amounts of text while viewing only those subsections that are currently of interest.

Does Vim support markdown?

Modern version of Vim and Neovim support folding . md Markdown documents on their section headings # , ## , ### etc.


2 Answers

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.

function! MarkdownLevel()     if getline(v:lnum) =~ '^# .*$'         return ">1"     endif     if getline(v:lnum) =~ '^## .*$'         return ">2"     endif     if getline(v:lnum) =~ '^### .*$'         return ">3"     endif     if getline(v:lnum) =~ '^#### .*$'         return ">4"     endif     if getline(v:lnum) =~ '^##### .*$'         return ">5"     endif     if getline(v:lnum) =~ '^###### .*$'         return ">6"     endif     return "="  endfunction au BufEnter *.md setlocal foldexpr=MarkdownLevel()   au BufEnter *.md setlocal foldmethod=expr      
like image 169
Jeromy Anglim Avatar answered Sep 20 '22 04:09

Jeromy Anglim


let g:markdown_folding = 1 

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.

set nofoldenable 

Update

Unfortunately, I stopped using markdown_folding since it makes things slow (Github issue).

like image 24
Sanghyun Lee Avatar answered Sep 17 '22 04:09

Sanghyun Lee