Neovim 101 — Literate Programming

Literate programming using Neovim and Tree-sitter.

alpha2phi
3 min readOct 2, 2022

--

Neovim 101 — Literate Programming

Literate programming is a methodology that combines a programming language with a documentation language, thereby making programs more robust, more portable, more easily maintained, and arguably more fun to write than programs that are written only in a high-level language.

In this article, we will use the Markdown language to write the documentation, and leverage Tree-sitter to generate the output of the code snippets.

This article is part of the Neovim 101 series.

The Neovim configuration files are available in this repository.

Getting Started

In the articles related to Emacs, we showed you how to execute source code within Org documents using Babel, which is an extension of Org Mode.

The Markdown language is still the most widely used format for technical documentation. Using Neovim and Tree-sitter, we can easily generate the code output as part of the documentation.

Let’s use the Python language as an example.

In the screenshot below, we have a Markdown document with multiple Python code snippets. Using Tree-sitter, we can generate the code output easily from within Neovim, with less than 60 lines of code.

Literate programming

Tree-sitter Query

We introduced to you the basics of Tree-sitter in the previous article.

Tree-sitter has a Markdown parser. Using the nvim-treesitter playground plugin, we create the…

--

--

alpha2phi

Software engineer, Data Science and ML practitioner.