From 2f60acd5625283b21636b4b9f764c5d3019d56df Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 28 Jan 2023 14:46:07 +1100 Subject: [PATCH] fix(#1946): only change vim's global cwd on startup when opening the tree --- lua/nvim-tree.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 2cf44e3d81c..12ccef4da99 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -267,12 +267,6 @@ function M.on_enter(netrw_disabled) local stats = vim.loop.fs_stat(bufname) local is_dir = stats and stats.type == "directory" local is_file = stats and stats.type == "file" - local cwd - if is_dir then - cwd = vim.fn.expand(vim.fn.fnameescape(bufname)) - -- INFO: could potentially conflict with rooter plugins - vim.cmd("noautocmd cd " .. vim.fn.fnameescape(cwd)) - end local lines = not is_dir and vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) or {} local buf_has_content = #lines > 1 or (#lines == 1 and lines[1] ~= "") @@ -309,6 +303,13 @@ function M.on_enter(netrw_disabled) end if should_open or should_hijack or existing_tree_wins[1] ~= nil then + local cwd + if is_dir then + cwd = vim.fn.expand(vim.fn.fnameescape(bufname)) + -- INFO: could potentially conflict with rooter plugins + vim.cmd("noautocmd cd " .. vim.fn.fnameescape(cwd)) + end + lib.open { path = cwd } if should_focus_other_window then