Preview the file backing the current Neovim buffer in your default browser,
powered by mo.
The plugin is deliberately small: it reads the current buffer's file name, calls
the mo CLI with that path, and opens the resulting URL. It does not copy buffer
contents to a temporary file or sync unsaved changes to mo.
:MoPreviewcommand to open the current buffer's file inmo.:MoPreview --closeto close the file again from the same group.:MoPreview --clearto empty a wholemoport in one call.:MoPreview!as a shorthand for--openthat always wins.- Uses the real file on disk, so
mo's native live-reload works on save. - Opens the exact file's deeplink, not just the
moserver root. - Supports
mogroups and custom ports. - Per-call flags
--target,--port, and--no-open/--openoverride the configured target, port, and open;--closeswitches the command to closing. - Puts each file in its own
mogroup named after its URI-encoded full path; pass--targetto share a group explicitly.
- Neovim 0.10 or newer (
vim.systemis required). mo1.7.0 or newer, available asmoon$PATH(or setcmdin the setup).
{
"linrongbin16/mo-markdown-preview.nvim",
cmd = { "MoPreview" },
opts = {},
}use({
"linrongbin16/mo-markdown-preview.nvim",
config = function()
require("mo-markdown-preview").setup()
end,
})Plug 'linrongbin16/mo-markdown-preview.nvim'git clone https://github.com/linrongbin16/mo-markdown-preview.nvim \
~/.local/share/nvim/site/pack/plugins/start/mo-markdown-preview.nvimOpen a file and run:
:MoPreviewThe default browser opens on that file in mo. Running :MoPreview again adds
the same file to its group (deduplicated by absolute path) and opens its URL
again.
By default each file gets its own group named after its full path (with forward
slashes on Windows), so opening several files does not mix them in one mo
sidebar. The path is URI-encoded with the RFC 3986 rules: URL-path characters
(letters, digits, and symbols such as -._~/:&+=) are kept, and everything else
(spaces, CJK characters, ?, #, %, control characters) becomes %XX, for
example a space becomes %20 and 中 becomes %E4%B8%AD.
All flags are optional. With no flags, target, port, and open come from
the plugin configuration (config.lua defaults, optionally overridden by
setup()); a nil target uses the URI-encoded full path.
Pass --target to choose a group yourself:
:MoPreview --target docs
:MoPreview --target defaultUse --port to talk to a mo server on a specific port for this call. It
overrides port from setup():
:MoPreview --port 6276Use --no-open to only report the preview URL without opening a browser:
:MoPreview --no-openUse --open to force opening the browser for this call, even when
open = false is configured:
:MoPreview --open! is a shorthand for --open and :MoPreview! is the same as
:MoPreview --open. --no-open and --open override each other, so the last
one wins, while ! has the highest priority: it wins no matter where --open
or --no-open appear, so :MoPreview! --no-open still opens the browser.
The target and port flags can be combined with one of the mode flags, and
--flag=value is accepted as well. Quote values that contain spaces:
:MoPreview --target docs --port 6276 --no-open
:MoPreview --target="my group"Close the file again with --close:
:MoPreview --close
:MoPreview --close --target docs
:MoPreview --close --port 6276Empty the whole mo port with --clear:
:MoPreview --clear
:MoPreview --clear --port 6276--close runs mo --close with the same target and port resolution as
opening, so the defaults match. If you opened with --target or --port,
pass the same flags (or configure them) to close the file. Opening and closing
are different modes, so --close conflicts with --open, --no-open, and
!; combining them is an error.
--clear runs mo --clear: it shuts the server down, drops the saved session
for that port, and starts a fresh, empty server on the same port. Already open
preview tabs reconnect to the new server and show an empty list. Because it
works on a whole port instead of a file, it takes no buffer, needs no file on
disk, and conflicts with --close, --no-open, --open, !, and --target.
Only --port applies.
The command requires the buffer to be backed by a readable file. If it is not,
the plugin reports an error and does not call mo. Save the buffer to refresh
the preview. mo watches the file and reloads it, but unsaved edits are never
sent to mo.
setup() is optional; the defaults below are already active.
require("mo-markdown-preview").setup({
-- Command used to run mo: name on $PATH or absolute path.
cmd = "mo",
-- Default mo group. nil uses the URI-encoded full path.
-- Overridden by --target for one call.
target = nil,
-- mo server port. nil uses mo's default port (6275).
-- Overridden by --port for one call.
port = nil,
-- Open the preview URL in the default browser.
-- Overridden by --no-open / --open / ! for one call.
open = true,
})| Option | Default | Description |
|---|---|---|
cmd |
"mo" |
Command used to run mo: name or absolute path. |
target |
nil |
Default mo group. nil uses the URI-encoded full path. |
port |
nil |
mo --port. nil means mo's default (6275). --port overrides it. |
open |
true |
Open the URL in the default browser. Set false to only show the URL. |
Command flags override these options for a single call: :MoPreview accepts
--target, --port, --close, --clear, --no-open, and --open, plus the
! modifier as a higher-priority --open. --close conflicts with --open,
--no-open, and !. --clear conflicts with all of them and with
--target.
The Lua API is also available:
local mo = require("mo-markdown-preview")
mo.open() -- open the current buffer
mo.open({ bufnr = 3 }) -- another buffer
mo.open({ target = "docs" }) -- explicit mo group
mo.open({ bufnr = 3, target = "api" })
mo.open({ port = 6276 }) -- one call on another port
mo.open({ open = false }) -- only report the URL
mo.open({ open = true }) -- force opening the browser for this call
mo.open({ target = "docs", port = 6276, open = false })
mo.close() -- close the current file
mo.close({ target = "docs" }) -- close from a specific group
mo.close({ port = 6276 }) -- close on another port
mo.clear() -- empty the configured port
mo.clear({ port = 6276 }) -- empty another port- Get the file name from the current buffer.
- Check that the path exists and is readable. If not, report an error.
- Run
mo --no-open --json [--port PORT] --target GROUP <file>.PORTcomes from--port, then theportoption.GROUPcomes from--target, then thetargetoption; when both are nil,GROUPis the file's full path (with forward slashes on Windows) URI-encoded with RFC 3986; URL-path characters are kept and everything else becomes%XX. - Parse
mo's JSON output, find the deeplink for<file>, and open it withvim.ui.open. With--no-open(oropen = false) the URL is only reported.
mo always runs with --no-open; the --open flag and the ! modifier only
control whether the plugin calls vim.ui.open. That keeps a single call to one
browser tab, and the tab always lands on the exact file's deeplink.
:MoPreview --close runs mo --close [--port PORT] --target GROUP <file> with
the same target and port resolution, then reports success or the error from
mo.
:MoPreview --clear runs mo --clear [--port PORT] with y written to its
standard input. mo exits 0 both when it clears and when it cancels, so the
plugin reads mo's stderr to tell the two apart and reports which one happened.
No file is written, no buffer contents are copied, and no autocmds are
installed. mo reads the file from disk and handles reloading when Neovim saves
it.
The plugin does not manage the mo server lifecycle by itself: it never
shuts the server down unless you ask for it. mo is a shared, long-lived
preview server that restores sessions across invocations. Use
:MoPreview --clear to empty a port and restart its server, and
mo --shutdown to stop it without touching the saved session.
- The default group is the URI-encoded full path. Characters allowed in a URL
path (letters, digits, and symbols such as
-._~/:&+=) are kept; spaces, CJK characters,?,#,%, and control characters become%XX(a space becomes%20,中becomes%E4%B8%AD). Themosidebar shows the encoded group name, while file entries keep their real names. Encoding matters becausemocompares the group name with the percent-encoded URL path without decoding it, and it is injective, so distinct files always get distinct groups. - Unsaved changes are not previewed. Save the file and let
moreload it. - Buffers without a file on disk (unnamed buffers,
oil://, terminal buffers, and so on) are rejected with an error message. - On Windows,
moservers started in WSL (or in any other environment that shares the port) cannot read Windows paths such asC:\.... The Windowsmoclient still exits 0 when such a server rejects the file, so the plugin reads themo: added 0 item(s)line on stderr and reports it, naming the port. Stop the other server withmo --shutdownin that environment, or preview on a free port with--portor theportoption.
The tests run with headless Neovim and a fake mo executable, so they do not
need mo or a browser:
make testRun them from a POSIX shell (Linux, macOS, or WSL): the fake mo is a shell
script, and Windows cannot execute it, so every test that calls mo fails
there. On Windows, run make test from WSL against the checkout on /mnt/c.
Lua sources are formatted with StyLua
using the settings in .stylua.toml (2-space indent, 120 columns):
make fmt # reformat lua/ plugin/ tests/
make fmt-check # fail if anything is unformattedMIT