Current dir does not change when opening a file #2062
-
DescriptionSteps: Result: Expected: my config:
Neovim version
Operating system and versionLinux fedora 6.1.14-200.fc37.x86_64 nvim-tree versionUpdated nvim-tree/nvim-tree.lua: bbb6d48..1d79a64 Minimal config-- disable netrw at the very start of your init.lua (strongly advised)
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
nvim_tree.setup {
respect_buf_cwd = true,
sync_root_with_cwd = true,
} Steps to reproduce
Expected behaviornvim tree opens in Documents folder Actual behaviornvim tree opens in Downloads folder |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 5 replies
-
I was trying to solve this but is so annoying. As far as my limited knowledge about lua, nvimtree and programming itself allows me; the main problem is that the autocmd is running change_dir.fn() on every bufEnter, so when you focus nvimtree again, it switches to the first root directory of that instance. It has to have a simple solution. |
Beta Was this translation helpful? Give feedback.
-
You can have it open the tree in
|
Beta Was this translation helpful? Give feedback.
-
I don't mean to be rude @alextrastero ; are you new to vim? The title of the document suggests so... It appears we have a misunderstanding of expectations here. In any case, you can handle startup directories etc. yourself: see wiki: Open At Startup for recipes to achieve this. |
Beta Was this translation helpful? Give feedback.
-
At risk of also being called "new to vim", I'm wondering if someone could spell out exactly how to achieve the behavior @alextrastero was trying to get. FWIW, when reading the documentation, I was also under the impression that respect_buf_cwd would behave as alextrastero described. Anyway, I'm unsure of how to configure nvim-tree so that when you open it, it shows the contents of the directory of the file you currently have open in your buffer. Is there an autocommand that can be configured to do this? Something like:
|
Beta Was this translation helpful? Give feedback.
-
Very forcefully change the global cwd before opening, which will be in the new cwd: vim.keymap.set("n", "<Leader>a", function()
vim.cmd.cd(vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":h"))
api.tree.open({
find_file = true,
})
end, { noremap = true }) Set As above, there are many ways to achieve this behaviour. See #2350 (comment) |
Beta Was this translation helpful? Give feedback.
-
Looking back over this discussion, it seems that the deficiency might be around *nvim-tree.update_focused_file.update_root*
(previously `update_focused_file.update_cwd`)
Update the root directory of the tree if the file is not under current
root directory. It prefers vim's cwd and `root_dirs`.
Otherwise it falls back to the folder containing the file.
Only relevant when `update_focused_file.enable` is `true`
Type: `boolean`, Default: `false` A means of always changing the tree root may be useful. |
Beta Was this translation helpful? Give feedback.
-
@alex-courtis thanks very much for your help with this, I appreciate it! I was actually a little unclear in my question. What I really wanted was a single keymap that would both toggle nvim-tree and change to the directory of the file you currently have open in your buffer. That said, I was able to figure out the rest of it from your examples and came up with the following. First, I created this function in my nvim-tree.lua file: local M = {}
M.cwd_to_buf_and_toggle = function()
local status_ok, nvimtree_api = pcall(require, "nvim-tree.api")
if not status_ok then
vim.notify("WARNING: nvim-tree.lua failed to load.")
return
end
nvimtree_api.tree.find_file({ buf = vim.api.nvim_buf_get_name(0), update_root = true, focus = true })
nvimtree_api.tree.toggle()
end
return M Then, in my keymap file I have a keymap that calls the cwd_to_buf_and_toggle() function, e.g.: vim.keymap.set("n", "<Leader>e", "<cmd>lua require('nvim-tree').cwd_to_buf_and_toggle()<CR>", silent=true) Obviously, I don't need to export the function and define the keymap in a different file for this to work, but I like to have my keymaps all in one place for easier management. |
Beta Was this translation helpful? Give feedback.
-
Today, I faced kind of the same need, being on my home directory I wanted to open the ...and from there, I did this:
Result: Expected result: Doing some googling, I found that there are a lot of people asking for the same thing and tried multiple suggestions but the one that really worked like a charm was this one I found in Reddit and that I tweaked a little bit for my Lua config: vim.api.nvim_create_autocmd('VimEnter', {
desc = 'Open Nvim-Tree on the given directory from command line',
group = vim.api.nvim_create_augroup('NvimTreeSetGivenDirectory', { clear = true }),
pattern = '*',
command = "if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') | wincmd p | enew | execute 'cd '.argv()[0] | endif",
})
|
Beta Was this translation helpful? Give feedback.
-
Please see Open At Startup |
Beta Was this translation helpful? Give feedback.
Please see Open At Startup