From c4aa09b6bef96eea38044b60bd2eeb94987f0a22 Mon Sep 17 00:00:00 2001 From: rootiest Date: Tue, 18 Mar 2025 22:26:49 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat(osc7):=20cwd=20tracking=20and?= =?UTF-8?q?=20matching?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Writes the CWD to a file (and emits the OSC7) at startup and when the directory changes. This is used by a function in fish-shell to automatically cd to the cwd on nvim exit. --- lua/data/autocmd.lua | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lua/data/autocmd.lua b/lua/data/autocmd.lua index 64ef164..8dfc744 100644 --- a/lua/data/autocmd.lua +++ b/lua/data/autocmd.lua @@ -128,4 +128,40 @@ autocmd({ 'BufEnter' }, { end, }) +-- Update OSC7 in fish shell +local function update_osc7() + local cwd = vim.fn.getcwd() + local uri = 'file://' .. vim.fn.hostname() .. cwd + io.stdout:write('\27]7;' .. uri .. '\27\\') +end + +-- Save CWD where we can access it +local function save_cwd() + local cwd = vim.fn.getcwd() + local file = io.open(os.getenv('HOME') .. '/.nvim_last_dir', 'w') + if file then + file:write(cwd) + file:close() + end +end + +-- Update OSC7 and file on directory change +autocmd('DirChanged', { + callback = function() + save_cwd() + update_osc7() + end, +}) + +-- Save on exit +autocmd('VimLeavePre', { + callback = save_cwd, +}) + +-- Send OSC 7 on startup +update_osc7() + +-- Save CWD on startup +save_cwd() + return M