<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jan Toth</title>
    <description>The latest articles on DEV Community by Jan Toth (@xjantoth).</description>
    <link>https://dev.to/xjantoth</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F294036%2F29c057cf-546f-4c16-a308-290b2a48295c.png</url>
      <title>DEV Community: Jan Toth</title>
      <link>https://dev.to/xjantoth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xjantoth"/>
    <language>en</language>
    <item>
      <title>How to generate TOC by using vim function</title>
      <dc:creator>Jan Toth</dc:creator>
      <pubDate>Thu, 16 Apr 2020 07:57:03 +0000</pubDate>
      <link>https://dev.to/xjantoth/how-to-generate-toc-by-using-vim-function-2djd</link>
      <guid>https://dev.to/xjantoth/how-to-generate-toc-by-using-vim-function-2djd</guid>
      <description>&lt;p&gt;I had this requirement when writing &lt;strong&gt;README.md&lt;/strong&gt; file to generate &lt;em&gt;TOC&lt;/em&gt; (Table Of Content) automatically in &lt;strong&gt;vim&lt;/strong&gt; right above the particular &lt;strong&gt;### 34. Title XYZ&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/xjantoth/helmfile-course/blob/master/img/vim-trick.svg"&gt;https://github.com/xjantoth/helmfile-course/blob/master/img/vim-trick.svg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's pretend here is &lt;strong&gt;my title&lt;/strong&gt; in markdown.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;vim README.md

...
&lt;span class="c"&gt;### 20. This is my title I would like to generate TOC for&lt;/span&gt;

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 &lt;span class="nb"&gt;type &lt;/span&gt;and scrambled it to make a &lt;span class="nb"&gt;type &lt;/span&gt;specimen book. 
It has survived not only five centuries, but publishing 
software like Aldus PageMaker including versions of Lorem Ipsum.

...
:wq!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here is the code snippet you need to paste in your &lt;code&gt;~/.vimrc&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="c"&gt;" quick .vimrc access with ,t&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt; Toc&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; $&lt;span class="k"&gt;file&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; @%
  &lt;span class="k"&gt;let&lt;/span&gt; $linenr &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
  &lt;span class="k"&gt;w&lt;/span&gt;
  &lt;span class="c"&gt;"echo system('sed -i -E "${linenr}s/^[#]{2,} (.*)/&amp;lt;!-- - [\1](#\L\1) -&amp;gt;/; :a s/(\(#[^ ]+) /\1-/g; ta" $file')&lt;/span&gt;
  echo &lt;span class="nb"&gt;system&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sed -i -E  -e "${linenr}s/^[#]{2,} (.*)/&amp;lt;!-- - [\1](#\L\1) -&amp;gt;/; :a s/(\(#[^ ]+) /\1-/g; ta" -e "s/(.*)(\(#[0-9]{1,2})(\.)(.*)/\1\2\4/g" $file'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;e&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;
&lt;span class="k"&gt;endfunction&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; mapleader &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;","&lt;/span&gt;
&lt;span class="c"&gt;" quick .vimrc access with ,t&lt;/span&gt;
nmap &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;leader&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;t&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;call&lt;/span&gt; Toc&lt;span class="p"&gt;()&amp;lt;&lt;/span&gt;CR&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Open up your &lt;strong&gt;README.md&lt;/strong&gt; file once again and navigate to line:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(&lt;code&gt;### 20. What is Lorem Ipsum?&lt;/code&gt;) with your title.&lt;/li&gt;
&lt;li&gt;hit &lt;code&gt;,t&lt;/code&gt; keys + &lt;code&gt;Enter&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;vim README.md

...
&lt;span class="c"&gt;### 20. This is my title I would like to generate TOC for&lt;/span&gt;

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 &lt;span class="nb"&gt;type &lt;/span&gt;and scrambled it to make a &lt;span class="nb"&gt;type &lt;/span&gt;specimen book. 
It has survived not only five centuries, but publishing 
software like Aldus PageMaker including versions of Lorem Ipsum.

...
:wq!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;vim README.md
...

&amp;lt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nt"&gt;--&lt;/span&gt; - &lt;span class="o"&gt;[&lt;/span&gt;20. This is my title I would like to generate TOC &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="c"&gt;#20-this-is-my-title-i-would-like-to-generate-toc-for)--&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;### 20. This is my title I would like to generate TOC for&lt;/span&gt;

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 &lt;span class="nb"&gt;type &lt;/span&gt;and scrambled it to make a &lt;span class="nb"&gt;type &lt;/span&gt;specimen book. 
It has survived not only five centuries, but publishing 
software like Aldus PageMaker including versions of Lorem Ipsum.
...

:wq!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Check my other work at &lt;a href="https://www.udemy.com/course/learn-devops-helm-helmfile-kubernetes-deployment/?couponCode=DEVTO_APRIL"&gt;Udemy&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.udemy.com/course/learn-aws-eks-kubernetes-cluster-and-devops-in-aws-part-1/?couponCode=AWSEKSJUN"&gt;Learn AWS EKS Kubernetes cluster and devops in AWS (Part 1)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.udemy.com/course/learn-kubernetes-docker-devops-and-helm-charts-from-scratch/?couponCode=KUBEADMJUN"&gt;Learn Kubernetes Docker/DevOps and helm charts from scratch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.udemy.com/course/learn-devops-helm-helmfile-kubernetes-deployment/?couponCode=HELMJUN"&gt;Learn DevOps Helm/Helmfile Kubernetes deployment&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.udemy.com/course/learn-devops-kubernetes-deployment-by-kops-and-terraform/?referralCode=D3FA7AD01325223ADBDE"&gt;Learn Devops Kubernetes deployment by kops and terraform&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vim</category>
      <category>devops</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
