如何设置nvim-tree默认打开当前目录而非主目录?
解决nvim-tree打开时显示主目录及
open_on_setup_files属性未知问题 问题原因
open_on_setup_files是nvim-tree旧版本的配置项,在新版中已被移除,因此会提示属性未知。
解决方案
根据你的需求选择对应配置:
需求1:打开nvim-tree时自动同步当前工作目录(而非固定主目录)
在nvim_tree.setup()中添加同步工作目录的配置项:
sync_root_with_cwd = true, respect_buf_cwd = true, update_focused_file = { enable = true, update_root = true -- 聚焦文件时更新根目录 }
修改后的完整配置代码:
local status, nvim_tree = pcall(require, "nvim-tree") if not status then vim.notify("没有找到 nvim-tree") return end -- 列表操作快捷键 local list_keys = require('keybindings').nvimTreeList nvim_tree.setup({ -- 不显示 git 状态图标 git = { enable = false, }, -- 同步根目录与当前工作目录 sync_root_with_cwd = true, respect_buf_cwd = true, -- project plugin 需要这样设置 update_cwd = true, update_focused_file = { enable = true, update_cwd = true, update_root = true -- 新增:更新根目录为当前文件所在目录 }, -- 隐藏 .文件 和 node_modules 文件夹 filters = { dotfiles = true, custom = { 'node_modules' }, }, view = { -- 宽度 width = 35, -- 也可以 'right' side = 'left', -- 隐藏根目录 -- hide_root_folder = false, -- 显示行数 number = true, relativenumber = true, -- 显示图标 signcolumn = 'yes', }, --on_attach = { --custom_only = false, --list = list_keys, --}, actions = { open_file = { -- 首次打开大小适配 resize_window = true, -- 打开文件时关闭 quit_on_open = true, }, }, }) -- 自动关闭 vim.cmd([[ autocmd BufEnter * ++nested if winnr('$') == 1 && bufname() == 'NvimTree_' . tabpagenr() | quit | endif ]])
需求2:每次打开nvim-tree都默认显示主目录
如果需要固定打开主目录,在配置末尾添加自动命令:
vim.api.nvim_create_autocmd("VimEnter", { callback = function() require("nvim-tree.api").tree.open(vim.fn.expand("$HOME")) end })
内容的提问来源于stack exchange,提问作者Hu Yifan
相关产品推荐
相关产品推荐

