DEV Community

rderik
rderik

Posted on

Generating a table of contents with anchors for markdown - Vim plugin

I use vim as my editor and markdown to write the articles for my site. I think that the articles are easier to navigate when they have a table of contents.

Adding a table of contents was easy. Many plugins accomplish this. But I couldn't find a plugin that also added the anchors. And adding the anchors was a very tedious process. So I modified the vim-markdown-toc plugin to add the anchors.

If you are looking to do the same, I wrote a short post on how to do it with my modified version of the plugin. You can read about it here:

https://rderik.com/notes/generate-table-of-contents-with-anchors-for-markdown-file-vim-plugin/

I hope you find it useful.

Top comments (2)

Collapse
 
pbnj profile image
Peter Benjamin (they/them) • Edited

Nice. I used to use that plugin.

The beauty of vim, for me, is its adherence to the Unix Philosophy, which allows me to call external programs from within vim to accomplish something.

In this use-case, I can use doctoc CLI in any number of ways:

  • :! doctoc %
  • :TocGH/:TocGL/:TocBB commands and custom function that call the respective doctoc flags (which can subsequently be automatically called on BufWritePre event on markdown filetypes)
  • :make md-toc make target that calls doctoc .
  • or finally as a pre-commit git hook

Another demonstration of vim's composability is this blog article posted a few days ago

Collapse
 
rderik profile image
rderik

Yea, you are right, I like that about vim. Another example of that composition I use every now and then is editing a file in binary form:

:%!xxd

And then back:

::%!xxd -r