Last active
July 3, 2025 19:28
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use:
init.lua:sourceNow 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!)