diff --git a/README.md b/README.md index 782a18d3..4a89130c 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ let g:nvim_tree_lsp_diagnostics = 1 "0 by default, will show lsp diagnostics in let g:nvim_tree_disable_window_picker = 1 "0 by default, will disable the window picker. let g:nvim_tree_hijack_cursor = 0 "1 by default, when moving cursor in the tree, will position the cursor at the start of the file on the current line let g:nvim_tree_icon_padding = ' ' "one space by default, used for rendering the space between the icon and the filename. Use with caution, it could break rendering if you set an empty string depending on your font. +let g:nvim_tree_update_cwd = 0 "1 by default, will update the tree cwd when changing nvim's directory (DirChanged event). Behaves strangely with autochdir set. let g:nvim_tree_window_picker_exclude = { \ 'filetype': [ \ 'packer', diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 2b3c1ad6..1b86313a 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -297,6 +297,12 @@ of the file on the current line. One space by default, used for rendering the space between the icon and the filename. Use with caution, it could break rendering if you set an empty string depending on your font. +|g:nvim_tree_update_cwd| + +Can be 0 or 1. 1 by default. +Will update the tree cwd when changing nvim's directory (DirChanged event). +WARNING: Behaves strangely with autochdir set. + ============================================================================== INFORMATIONS *nvim-tree-info* diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 7dd91cf7..baca7955 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -128,7 +128,7 @@ function M.on_enter() local disable_netrw = vim.g.nvim_tree_disable_netrw or 1 local hijack_netrw = vim.g.nvim_tree_hijack_netrw or 1 if is_dir then - api.nvim_command('cd '..bufname) + api.nvim_command('lcd '..bufname) end local should_open = vim.g.nvim_tree_auto_open == 1 and ((is_dir and (hijack_netrw == 1 or disable_netrw == 1)) or bufname == '') diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index c3d584a8..f2b735aa 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -410,7 +410,7 @@ function M.change_dir(foldername) return end - vim.cmd('cd '..foldername) + vim.cmd('lcd '..foldername) M.Tree.entries = {} M.init(false, true) end diff --git a/plugin/tree.vim b/plugin/tree.vim index 4cfeffee..5700765b 100644 --- a/plugin/tree.vim +++ b/plugin/tree.vim @@ -29,6 +29,9 @@ augroup NvimTree if get(g:, 'nvim_tree_hijack_cursor', 1) == 1 au CursorMoved NvimTree lua require'nvim-tree'.place_cursor_on_node() endif + if get(g:, 'nvim_tree_update_cwd', 1) == 1 + au DirChanged * lua require'nvim-tree.lib'.change_dir(vim.loop.cwd()) + endif augroup end command! NvimTreeOpen lua require'nvim-tree'.open()