How to generate TOC by using vim function
Tags: vim, devops, functional
Published: 2020-04-16 07:57:03

I had this requirement when writing **README.md** file to generate *TOC* (Table Of Content) automatically in **vim** right above the particular **### 34. Title XYZ**

https://github.com/xjantoth/helmfile-course/blob/master/img/vim-trick.svg

Let's pretend here is **my title** in markdown.

```bash
vim README.md

...
### 20. This is my title I would like to generate TOC for

Lorem Ipsum is simply dummy text of the printing 
and typesetting industry. Lorem Ipsum has been 
the industrys standard dummy text ever since 
the 1500s, when an unknown printer took a galley 
of type and scrambled it to make a type specimen book. 
It has survived not only five centuries, but publishing 
software like Aldus PageMaker including versions of Lorem Ipsum.

...
:wq!
```

Here is the code snippet you need to paste in your `~/.vimrc`

```vim
" quick .vimrc access with ,t
function! Toc()
  let $file = @%
  let $linenr = line(".")
  .t.
  w
  "echo system('sed -i -E "${linenr}s/^[#]{2,} (.*)/<!-- - [\1](#\L\1) ->/; :a s/(\(#[^ ]+) /\1-/g; ta" $file')
  echo system('sed -i -E  -e "${linenr}s/^[#]{2,} (.*)/<!-- - [\1](#\L\1) ->/; :a s/(\(#[^ ]+) /\1-/g; ta" -e "s/(.*)(\(#[0-9]{1,2})(\.)(.*)/\1\2\4/g" $file')
  e!
endfunction

let mapleader = ","
" quick .vimrc access with ,t
nmap <leader>t :call Toc()<CR>
```

Open up your **README.md** file once again and navigate to line:
* (`### 20. What is Lorem Ipsum?`) with your title.
* hit `,t` keys + `Enter`

```bash
vim README.md

...
### 20. This is my title I would like to generate TOC for

Lorem Ipsum is simply dummy text of the printing 
and typesetting industry. Lorem Ipsum has been 
the industrys standard dummy text ever since 
the 1500s, when an unknown printer took a galley 
of type and scrambled it to make a type specimen book. 
It has survived not only five centuries, but publishing 
software like Aldus PageMaker including versions of Lorem Ipsum.

...
:wq!
```

Output:

```bash
vim README.md
...

<!-- - [20. This is my title I would like to generate TOC for](#20-this-is-my-title-i-would-like-to-generate-toc-for)-->
### 20. This is my title I would like to generate TOC for

Lorem Ipsum is simply dummy text of the printing 
and typesetting industry. Lorem Ipsum has been 
the industrys standard dummy text ever since 
the 1500s, when an unknown printer took a galley 
of type and scrambled it to make a type specimen book. 
It has survived not only five centuries, but publishing 
software like Aldus PageMaker including versions of Lorem Ipsum.
...

:wq!
```
**Check my other work at [Udemy](https://www.udemy.com/course/learn-devops-helm-helmfile-kubernetes-deployment/?couponCode=DEVTO_APRIL)**
[Learn AWS EKS Kubernetes cluster and devops in AWS (Part 1)](https://www.udemy.com/course/learn-aws-eks-kubernetes-cluster-and-devops-in-aws-part-1/?couponCode=AWSEKSJUN)

[Learn Kubernetes Docker/DevOps and helm charts from scratch](https://www.udemy.com/course/learn-kubernetes-docker-devops-and-helm-charts-from-scratch/?couponCode=KUBEADMJUN)

[Learn DevOps Helm/Helmfile Kubernetes deployment](https://www.udemy.com/course/learn-devops-helm-helmfile-kubernetes-deployment/?couponCode=HELMJUN)

[Learn Devops Kubernetes deployment by kops and terraform](https://www.udemy.com/course/learn-devops-kubernetes-deployment-by-kops-and-terraform/?referralCode=D3FA7AD01325223ADBDE)