Skip to content

Instantly share code, notes, and snippets.

@eamonburns
Last active July 3, 2025 19:28
Show Gist options
  • Select an option

  • Save eamonburns/8a0b8e0e5151fa83bee124c742c78f6e to your computer and use it in GitHub Desktop.

Select an option

Save eamonburns/8a0b8e0e5151fa83bee124c742c78f6e to your computer and use it in GitHub Desktop.
Simple function to convert a file opened in NeoVim to HTML, and then open it in your browser
--- SPDX-License-Identifier: MIT
---Convert buffer in window to HTML, and open it in default browser.
---`winid` and `opt` are passed directly to `tohtml.tohtml`
---@param winid? integer # Window to convert (defaults to current window).
---@param opt? vim.tohtml.opt # Optional parameters.
function OpenHtml(winid, opt)
-- Make sure `tohtml.tohtml` is available
if vim.fn.has("nvim-0.10.0") == 0 then
print("`tohtml` function is only available after NeoVim 0.10")
return
end
local tohtml = require("tohtml").tohtml
-- Open a temporary file
local tmp_name = os.tmpname() .. ".html"
local tmp_file = assert(io.open(tmp_name, "w"))
-- Convert given window (default is current window) to HTML lines
local html_lines = tohtml(winid, opt)
-- Write lines to the temporary file (separated by newlines)
tmp_file:write(table.concat(html_lines, "\n"))
tmp_file:close()
-- Open temporary file in default program for `.html` files
vim.ui.open(tmp_name)
end
@eamonburns
Copy link
Author

How to use:

  • Option 1: Place the contents of this file in your init.lua
  • Option 2: Open this file in NeoVim, and then run :source

Now you can run :lua OpenHtml() from within NeoVim, and the currently open file will open in your browser (with all your fancy colors and styling!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment