From 26dd7dbf3ef14a146e0658c6adee987ab7925527 Mon Sep 17 00:00:00 2001 From: phanium <91544758+phanen@users.noreply.github.com> Date: Mon, 6 Oct 2025 22:12:09 +0800 Subject: [PATCH 1/3] fix: prevent NvimTree to be alternate buffer when tab open --- lua/nvim-tree/actions/node/open-file.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index de43416eac1..094b89453ba 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -198,7 +198,14 @@ local function open_file_in_tab(filename) if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) end - vim.cmd("tabe " .. vim.fn.fnameescape(filename)) + vim.cmd.tabnew() + -- HACK: prevent NvimTree to be alternate buffer + if utils.is_nvim_tree_buf(vim.fn.bufnr("#")) then + local tmpbuf = vim.api.nvim_create_buf(false, true) + vim.fn.setreg("#", tmpbuf) + vim.api.nvim_buf_delete(tmpbuf, { force = true }) + end + vim.cmd.edit(vim.fn.fnameescape(filename)) end local function drop(filename) From e922baab5e3401e871d97de3c95eca411b20551c Mon Sep 17 00:00:00 2001 From: phanium <91544758+phanen@users.noreply.github.com> Date: Mon, 6 Oct 2025 22:42:36 +0800 Subject: [PATCH 2/3] fix: prevent tabnew leave a dangling "[No Name]" buffer --- lua/nvim-tree/actions/node/open-file.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 094b89453ba..a17aa25a057 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -199,6 +199,7 @@ local function open_file_in_tab(filename) filename = utils.path_relative(filename, vim.fn.getcwd()) end vim.cmd.tabnew() + vim.bo.bufhidden = "wipe" -- HACK: prevent NvimTree to be alternate buffer if utils.is_nvim_tree_buf(vim.fn.bufnr("#")) then local tmpbuf = vim.api.nvim_create_buf(false, true) From edf6ef5e861b9d8ddc1fc751dfa3db190d6523bc Mon Sep 17 00:00:00 2001 From: phanium <91544758+phanen@users.noreply.github.com> Date: Tue, 7 Oct 2025 09:40:08 +0800 Subject: [PATCH 3/3] Update lua/nvim-tree/actions/node/open-file.lua Co-authored-by: Alexander Courtis --- lua/nvim-tree/actions/node/open-file.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index a17aa25a057..600f8656fbb 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -200,7 +200,7 @@ local function open_file_in_tab(filename) end vim.cmd.tabnew() vim.bo.bufhidden = "wipe" - -- HACK: prevent NvimTree to be alternate buffer + -- Following vim.fn.tabnew the # buffer may be set to the tree buffer. There is no way to clear the # buffer via vim.fn.setreg as it requires a valid buffer. Clear # by setting it to a new temporary scratch buffer. if utils.is_nvim_tree_buf(vim.fn.bufnr("#")) then local tmpbuf = vim.api.nvim_create_buf(false, true) vim.fn.setreg("#", tmpbuf)