DEV Community

loading...
Cover image for Markdown Preview in Vim

Markdown Preview in Vim

Rittik Dasgupta
I am a student, developer
・1 min read

When we edit a markdown file in vim, we are not able to preview the markdown file simultaneously. I have also faced this situation, so I decided why not make one.

To be able to preview a markdown, first, we need a markdown parser.
I got to know about Glow, which renders markdown on the Command Line Interface(CLI).

To install glow, you can follow the instructions given in the Github repo of Glow.

After installing Glow I added a few lines to my vimrc

Vim Function

This function finds the path of the current file and adds it in the .lastpreview.log file. After that bel vert terminal command is executed, which opens up the terminal buffer in vim, side by side.

nmap <F4> : call PreviewerMarkdown()<CR>clear<CR>glo $(cat ~/.lastpreview.log)<CR>
Enter fullscreen mode Exit fullscreen mode

Also, I made this custom mapping in vimrc. I bound my F4 key to the PreviewerMarkdown(). After PreviewerMarkdown is executed the clear command runs which clears the terminal buffer, and after this glo command runs, which is basically an alias I made for glow -s dark | less. The glo command opens the file path present in the lastpreview.log file with a pager (here less) and a suitable color scheme, and boom our markdown preview is now ready to be used.

There is room for adding more functionality to the PreviewerMarkdown(), but for this post, I chose to keep only the core functionality.

Discussion (0)