jdhao / nvim-config

A modern Neovim configuration with full battery for Python, Lua, C++, Markdown, LaTeX, and more...
MIT License
3.4k stars 502 forks source link

Open directory with nvim #257

Closed houtan-rocky closed 7 months ago

houtan-rocky commented 9 months ago

Looks like the nvim . for opening directory doesn't work, however it's a default command of nvim? what's replacement.

jdhao commented 7 months ago

Hello @mast9rmind , according to documentation here, you can add the following code to lua/custom-autocmd.lua of this configuration:

local function open_nvim_tree(data)

  -- 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)

  -- change to the directory
  vim.cmd.cd(data.file)

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

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

I tested it and it works fine.