Notes from the Wired

Neovim Markdown Preview: Command not working

Published: August 20, 2024

Info

This post isn’t meant as an introduction to any of the topics discussed below. Instead, it’s a brief guide on how to make the Markdown-Preview plugin work.

Background

(Neo)vim is, for the most part, the only editor I use for all my needs -— whether it’s programming, editing configuration files, or taking notes. I write all of my notes in Markdown for several reasons: it’s widely compatible, has easy-to-understand syntax, is very readable even without rendering, and, through various flavors, allows for additional functionality.

Even though Markdown is quite readable in plain text, it’s helpful to render it for features like images and LaTeX. To do this, I use the Neovim plugin Markdown Preview.

Installation

If you’re using the Lazy plugin manager, you can install the Markdown Preview plugin by opening your Neovim configuration (in Lunarvim with + L + c) and adding the following to the list of plugins:

 1 {
 2    "iamcco/markdown-preview.nvim",
 3    ft = "markdown",
 4    build = function()
 5        vim.fn["mkdp#util#install"]()
 6    end,
 7    config = function()
 8      vim.cmd([[do FileType]])
 9      vim.cmd([[
10         function OpenMarkdownPreview (url)
11            let cmd = "firefox -P 'Clean' --new-window " . shellescape(a:url) . " &"
12            silent call system(cmd)
13         endfunction
14      ]])
15      vim.g.mkdp_browserfunc = "OpenMarkdownPreview"
16      vim.g.mkdp_theme = "dark"
17    end,
18  },

In the OpenMarkdownPreview function, make sure to replace firefox with the name of the browser you use. If you don’t have a browser profile named clean, you should also remove the -P 'Clean' option.

After restarting Neovim, the plugin should install automatically. If not, type :Lazy, press S to sync, and then I to install the plugin. You can now use the command :MarkdownPreview when inside a Markdown file, and your browser should open with the rendered Markdown file. If you’re interested in further configuring the plugin, check out its GitHub repository.

Command not Working

If you run the command and nothing happens – no error message and your browser doesn’t open—try typing :messages to see if there’s an error. If you see something like Error: Cannot find module 'tslib', follow these steps to fix it:

  1. Check your message log for a file path similar to /lvim/lazy/markdown-preview.nvim/app/lib/app/ (your path might differ).
  2. Go to where the plugin is installed:
1cd ~/lvim/lazy/markdown-preview.nvim/app/lib/app/
  1. Install the necessary dependencies:
1npm install
  1. Restart neovim, it should work now.

References: