From 041adb0cdfedb68b776ea1e16b2e33a1458c622e Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Wed, 8 Jul 2026 21:02:00 +0200 Subject: [PATCH] Fix opening in referred code in new nvim window MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From Claude: Root cause (two independent bugs): 1. Ghostty eats the +cmd argument. Ghostty's -e flag scans every token for a leading + (its own +action syntax, e.g. +new-window) and silently drops any it doesn't recognize — including nvim's +call cursor(...). I confirmed this by launching ghostty and dumping the child's actual argv: +call cursor(5,10) vanished completely, leaving only nvim and the filepath. Since the jump command never ran, nvim's own BufReadPost autocmd (nvim/init.lua:231-240, restores cursor to the last-visited " mark) was the only thing setting the cursor — explaining exactly what you saw: file opens fine, cursor lands wherever you last left off in that file. 2. Off-by-one indexing. LSP positions are 0-indexed; Vim's cursor() is 1-indexed. This was real but secondary — masked by bug #1 since the whole command was being dropped anyway. Fix (nvim/init.lua): added a shared open_in_new_window(filepath, line, col) helper that shell-quotes the +cmd and filepath into a single string and runs it via ghostty -e sh -c '' — so the +-prefixed token is buried inside one argv element instead of being its own, and survives ghostty's parser. Both gd and the quickfix handler now use it. --- nvim/init.lua | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/nvim/init.lua b/nvim/init.lua index 28833a1..5b8faf3 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -59,6 +59,21 @@ vim.lsp.config('basedpyright', { }) vim.lsp.enable('basedpyright') +-- Ghostty's `-e` strips any argument starting with `+` (it treats them as +-- its own `+action` CLI syntax), silently dropping nvim's `+cmd` args. Route +-- through `sh -c` so the `+cmd` is buried inside one shell-quoted string +-- instead of being its own top-level argv token. +local function shellquote(s) + return "'" .. s:gsub("'", "'\\''") .. "'" +end + +local function open_in_new_window(filepath, line, col) + local cmd = string.format('nvim %s %s', + shellquote(string.format('+call cursor(%d,%d)', line, col)), + shellquote(filepath)) + vim.fn.jobstart({ 'ghostty', '-e', 'sh', '-c', cmd }) +end + vim.api.nvim_create_autocmd("LspAttach", { callback = function(args) local bufnr = args.buf @@ -82,13 +97,9 @@ vim.api.nvim_create_autocmd("LspAttach", { local uri = location.uri or location.targetUri local range = location.range or location.targetSelectionRange local filepath = vim.uri_to_fname(uri) - local line = range.start.line - local col = range.start.character - vim.fn.jobstart({ - 'ghostty', '-e', 'nvim', - string.format('+call cursor(%d,%d)', line, col), - filepath - }) + local line = range.start.line + 1 + local col = range.start.character + 1 + open_in_new_window(filepath, line, col) end) end, { buffer = bufnr, desc = "Go to definition (new window)" }) @@ -116,11 +127,7 @@ vim.api.nvim_create_autocmd("FileType", { local col = qf_item.col vim.cmd('cclose') - vim.fn.jobstart({ - 'ghostty', '-e', 'nvim', - string.format('+call cursor(%d,%d)', line, col), - filepath - }) + open_in_new_window(filepath, line, col) end, { buffer = args.buf, desc = "Open reference in new window", nowait = true }) end, }) -- 2.54.0