How to generate TOC by calling external sh
Practical guide on how to generate a table of contents by calling an external bash script from within Vim.
This bash script extracts HTML comment markers from a file and strips the comment delimiters, producing a table of contents based on specially formatted comments in your README or documentation files.
1
2
3
4
5
cat ~/bin/tocreadme.sh
#!/bin/bash
grep "<\!--" $1 | sed -E 's/^(<!--)(.*)(-->)/\2/'
To use this script from within Vim, add a key mapping to your .vimrc that calls the external script and inserts its output at the cursor position.
1
2
3
4
5
6
7
cat ~/.vimrc
...
nmap <leader>c :read! sh tocreadme.sh ~/Documents/sbx/aws-eks-devopsinuse/README.md <CR>
...
:wq!
This post is licensed under CC BY 4.0 by the author.