CompMD Author's Guide. Written for the @compmd packages at 0.14.0, and following them as they change. Download the book as PDF

20. Writing in Neovim

A CompMD document is a text file, so any editor opens it. Neovim does more with it: it colours its components and params, says what is wrong as you type, completes a component's name and its params, shows the value of each {{…}} beside it, and draws the document in place, its headings, lists, tables and boxes as they will look, while the text you type stays as written. This chapter sets Neovim up for CompMD, then goes through writing there, then its settings. Emacs and Helix have chapters of their own; in any editor, the playground at compmd.dev/playground shows a document as it renders.

Quick setup#

You need Neovim 0.12 or later, git, and the tree-sitter command (which builds CompMD's grammars).

1. The language server. What reads your document is CompMD's language server, compmd-lsp: a program Neovim starts beside it, which reads the document as a site does and tells the editor what it found. Install it once, with whichever you have:

npm install -g @compmd/lsp   # with Node.js
cargo install compmd-lsp     # with Rust

or download its file for your computer from the latest release and put it on your PATH.

2. The plugin, comp-markdown/compmd.nvim, with nvim-treesitter's main branch. With lazy.nvim (LazyVim too: a file of its own under lua/plugins/):

return {
  { 'comp-markdown/compmd.nvim', dependencies = { 'nvim-treesitter/nvim-treesitter' } },
}

or with Neovim's own vim.pack:

vim.pack.add({ 'https://github.com/nvim-treesitter/nvim-treesitter', 'https://github.com/comp-markdown/compmd.nvim' })

The plugin has no setup() to call.

3. The grammars, once:

:TSInstall compmd

which installs compmd with the two it needs, compmd_inline and cel.

4. Open a .comp.md file. The server starts with it. :checkhealth vim.lsp lists compmd among the running servers; if it says compmd-lsp isn't executable, it isn't on your PATH yet.

How to use#

What you see#

Open a document, and it is drawn in place: a heading's #s show as its level, H1 to H6, its line in its colour; a code block's fences are set back and its language coloured; bullets and checkboxes are symbols; quotes have a bar and alerts their icon; a table is a box, its cells padded so the columns line up even when the text isn't aligned; a container's lines have a bar, │, down their left side, its opener and closer as written; the frontmatter's fences are rules and its lines set back.

The line the cursor is on shows its text as written, so you always edit what is there: move off it, and it is drawn again. Nothing is changed in the file: the drawing is only how Neovim shows it. In visual mode the text is shown as written.

Fixing a mistake#

A mistake is underlined where it is, with the diagnostic's code and message: a component that isn't imported, a param it doesn't take, a value of the wrong type. ]d and [d go from one to the next, and <C-w>d shows the whole message. When there is a fix, gra (the code action) offers it at the mistake: import the pack that has a component, add a registry. The diagnostics and their fixes has every code.

Adding a component#

Type :: and completion offers the components your document can use, from your project's folder and its installed packs; picking one from a pack adds its import to the frontmatter for you. Inside {…}, it offers the component's params and then their values; in an expression, md. paths and the functions you can call. K on a component shows its signature, the params it takes and their types (chapter 15), and the value of an interpolation or expression shows beside it, as the last render computed it, once inlay hints are on (below):

---
title: Harbour notes
---

# {{md.doc.title}}

shows = Harbour notes after the }}.

Tables#

Type | in a table, and its cells are padded so the pipes line up in the file; vim.lsp.buf.format() aligns every table. gra on a table offers to insert, delete or move a row or a column, or change a column's alignment. A table wider than the window is drawn narrowed to fit it, its cells wrapped in their columns, until the cursor is in it; in a window that doesn't wrap (:set nowrap, as LazyVim's are), its rows run on and scroll.

<C-]> on a link goes to its file or heading; grr on a heading lists every link to it; grn on a heading renames it, and every link to it follows, in every file of the project. A link to a file or a heading that isn't there is warned about. gO lists the document's headings, containers and definitions.

The preview#

:LspCompmdPreview (or the code action Open preview) opens the document in your browser, as a site renders it, and follows as you type, scrolling to the part you edit.

Markdown files#

The server and the drawing serve your .md files too. A .md file is CompMD when its frontmatter has an md key (chapter 1); otherwise it is plain Markdown, and gets what Markdown has: the drawing, tables aligned as you type, links and headings, and the preview. Its ::: stays text, as GitHub shows it. The code action Read as CompMD adds the key.

What it adds#

Without it, a .comp.md file is Markdown to Neovim: its components are plain text, a typo in a param shows only when the page is built, and a table lines up only if you line it up. With it, the editor knows what the site will make of the document as you type: what is wrong and how to fix it, what a component takes, what a value comes to, and how the page will look.

Configuration#

The server's settings go where any server's do, under compmd, and apply without a restart:

vim.lsp.config('compmd', {
  settings = {
    compmd = {
      render = {
        enabled = true, -- false: nothing drawn
        glyphs = 'unicode', -- or 'nerd' (Nerd Font icons), 'ascii'
        reveal = 'line', -- or 'element' (the whole element as written while the cursor is in it), 'off'
        modes = { 'n', 'i', 'c', 't' }, -- the modes drawn in, by their first letter
        heading = { marker = true, fill = true }, -- marker = false keeps the #s, set back
        code = { language = 'inline', fence = true }, -- or 'above', 'fence', 'off'
        table = { borders = true },
        container = { gutter = true },
        frontmatter = { rules = true, dim = true },
        markdownFiles = true, -- false: .md files served but not drawn
      },
      tables = { alignOnType = true, alignOnFormat = true },
      markdown = { enabled = true, wikiLinks = false },
      -- preview = { browser = 'firefox' }, -- the command the preview opens with, instead of the system's browser
    },
  },
})

Each element of the drawing (heading, code, table, list, checkbox, quote, callout, rule, container, component, param, frontmatter) takes false to leave it as written. The Developer's Guide's chapter 24 lists every setting.

Values beside expressions are inlay hints, which Neovim shows once asked to: vim.lsp.inlay_hint.enable() in your configuration.

Colours. Each style is a highlight group, CompmdHeading1 to CompmdHeading6, CompmdHeadingMarker, CompmdCodeFence, CompmdCodeLanguage, CompmdContainerBorder, CompmdComponent, CompmdParam and the others, linked to your colour scheme's Markdown and tree-sitter groups, so a scheme restyles them. Set one yourself to change it:

vim.api.nvim_set_hl(0, 'CompmdHeading1', { fg = '#e0af68', bold = true })

Another Markdown plugin. render-markdown.nvim and markview.nvim draw Markdown buffers too, and with both a .md file is drawn twice: set render = { markdownFiles = false } to keep CompMD's help in .md files without its drawing, or markdown = { enabled = false } to leave them to your other tools entirely.

Off. vim.lsp.enable('compmd', false) stops the server.

Next: writing in Emacs.