April 12, 2023

A vim script to mark folds on Markdown headers by default

You can write a script for vim to mark folds on Markdown headers automatically, by default, so you don’t have to manually set markers.

To do so add the following script1 to your .vimrc file:

" Mark folds on Markdown headers
function! MarkdownFolds()
    let thisline = getline(v:lnum)
    if match(thisline, '^##') >= 0
        return ">2"
    elseif match(thisline, '^#') >= 0
        return ">1"
        return "="
setlocal foldmethod=expr
setlocal foldexpr=MarkdownFolds()

Save your .vimrc file.

You should find the .vimrc file under the path ~/.vimrc.

Reload your buffer: :e, short for :edit.

Now folds should be marked. You can use zc, zm and other fold commands without having to add markers manually.

Run :h fold in vim to see all the fold commands.

  1. 1↩︎


I do self-funded research and I'm writing a book.

> What's the book about?

About technologies and epistemology.

Meaning, technologies can foster agency. No doubt. But I am also asking:

Can technologies make us blind?

I posted a summary of the prologue on the homepage: https://yctct.com/


vim markdown personal computing office applications wiki

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2023 by r. philip is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88