From 111f09fda826a3b48aae54d30ecf69e3181f9e77 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Thu, 19 Nov 2026 10:51:32 +0000 Subject: [PATCH] Add :Watch command --- plugin/commands.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/plugin/commands.lua b/plugin/commands.lua index c089e23..9e29d6a 100644 --- a/plugin/commands.lua +++ b/plugin/commands.lua @@ -249,3 +249,29 @@ vim.api.nvim_create_user_command('ResizeFixed', function(opts) vim.wo.winfixheight = true end end, { nargs = '?' }) + +-- :Watch a file for changes and immediately load them. +vim.api.nvim_create_user_command("Watch", function() + local bufnr = vim.api.nvim_get_current_buf() + local path = vim.api.nvim_buf_get_name(bufnr) + + local w, err = vim.uv.new_fs_event() + if not w then + return vim.notify("Watch: " .. err, vim.log.levels.ERROR) + end + + local ok, start_err = w:start(path, {}, vim.schedule_wrap(function(cb_err) + if cb_err or not vim.api.nvim_buf_is_loaded(bufnr) then + w:close() + return + end + vim.cmd("silent! checktime " .. bufnr) + end)) + if not ok then + w:close() + return vim.notify("Watch: " .. start_err, vim.log.levels.ERROR) + end + + vim.bo[bufnr].autoread = true -- buffer-local + vim.notify("Watching " .. vim.fn.fnamemodify(path, ":t")) +end, {})