Files
dotfiles/.config/nvim/lua/plugins/nvim-tree.lua
2026-02-06 17:17:48 +08:00

39 lines
1.6 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
return {
"nvim-tree/nvim-tree.lua",
version = "*",
lazy = false,
dependencies = { "nvim-tree/nvim-web-devicons" },
config = function()
require("nvim-tree").setup({
-- 这里可以放自定义设置,初始用默认即可
})
-- 设置一个常用快捷键:空格+e 打开/关闭文件树
-- 为 nvim-tree 设置一组实用快捷键
vim.keymap.set('n', '<leader>e', ':NvimTreeToggle<CR>', { desc = 'Toggle file tree' })
vim.keymap.set('n', '<leader>f', function()
local current_win = vim.api.nvim_get_current_win()
local current_buf = vim.api.nvim_win_get_buf(current_win)
local buf_ft = vim.api.nvim_buf_get_option(current_buf, 'filetype')
if buf_ft == 'NvimTree' then
-- 从文件树返回时,尝试回到之前编辑的窗口
vim.cmd('wincmd p')
-- 如果上一个窗口还是文件树(可能只有一个文件树窗口),就关闭它
local new_win = vim.api.nvim_get_current_win()
local new_buf = vim.api.nvim_win_get_buf(new_win)
local new_buf_ft = vim.api.nvim_buf_get_option(new_buf, 'filetype')
if new_buf_ft == 'NvimTree' then
vim.cmd('NvimTreeClose')
end
else
-- 保存当前窗口ID以便从文件树返回时能准确回来
vim.g.last_normal_win = current_win
vim.cmd('NvimTreeFindFile')
end
end, { desc = '智能切换: 文件⇄树' })
vim.keymap.set('n', '<leader>t', ':NvimTreeFocus<CR>', { desc = 'Focus on the file tree' })
end,
}