Skip to content

Commit

Permalink
Enable nvim-tree to open dir during startup
Browse files Browse the repository at this point in the history
Now when you open a directory when starting nvim, nvim-tree will be
opened to handle the directory correctly, see also issue #257.
  • Loading branch information
jdhao committed Jan 7, 2024
1 parent df9491b commit 8416508
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lua/config/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ local nvim_tree = require("nvim-tree")
nvim_tree.setup {
auto_reload_on_write = true,
disable_netrw = false,
hijack_cursor = false,
hijack_netrw = true,
hijack_cursor = false,
hijack_unnamed_buffer_when_opening = false,
open_on_tab = false,
sort_by = "name",
Expand Down
20 changes: 20 additions & 0 deletions lua/custom-autocmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,23 @@ api.nvim_create_autocmd("VimResized", {
desc = "autoresize windows on resizing operation",
command = "wincmd =",
})

local function open_nvim_tree(data)
-- check if buffer is a directory
local directory = vim.fn.isdirectory(data.file) == 1

if not directory then
return
end

-- create a new, empty buffer
vim.cmd.enew()

-- wipe the directory buffer
vim.cmd.bw(data.buf)

-- open the tree
require("nvim-tree.api").tree.open()
end

vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree })

0 comments on commit 8416508

Please sign in to comment.