Skip to content

nvim-tree

What is nvim-tree

  • A file explorer for Neovim written in Lua
  • A pluglin for Neovim

Installation for Neovim

  • through plugin manager LazyVim
  • create a nvim-tree.lua config file

nvim-tree Mappings

When with cursor in the nvim-tree Explorer:

  • g? Help
  • q Quit Help
  • E Expand All
  • f Live Filter: Start
  • F Live Filter: Close
  • H Toggle Filer: Dotfiles
  • W Collapse
  • BS Close Directory
  • Enter or o to open the file in a new buffer and place the cursor on the first line of the file
  • Tab to open the file in a new buffer while keeping the cursor in nvimtree, this for example is useful if you want to open several files at once. However, keep using Tab to open other files will dump the old buffer
  • <C-e> Open/Close Folder under cursor
  • <C-t> to open the file in a new tab that can be managed separately from the other buffers present
  • <C-v> to open the file in the buffer by dividing it vertically into two parts, if there was already an open file this will be displayed side by side with the new file - does not work on my config
  • <C-x> to open the file in the buffer by dividing it horizontally into two parts

TIP

Windows terminal uses ctrl-v for paste which conflicts with the default mapping for vertical split in nvim-tree resulting in the following error: Error executing lua: vim/_editor.lua:0: Vim:E21: Cannot make changes, 'modifiable' is off^@stack traceback:^@^I[C]: in function 'nvim_put'^@^Ivim/_editor.lua: in function <vim/_editor.lua:0> when trying to perform a vertical split. This can be fixed by remapping paste in windows terminal.

Go to Windows Terminal Settings -> Actions and then change the mapping for paste from ctrl-v to ctrl-shift-v.

Commands

  • See :h NvimTree
CommandDescription
:NvimTreeOpenOpen the tree.
:NvimTreeFocusOpen the tree if it is closed, and then focus on the tree.
:NvimTreeToggleOpen or close the tree. Takes an optional path argument.
:NvimTreeFindFileMove the cursor in the tree for the current buffer, opening folders if needed.
:NvimTreeCollapseCollapses the nvim-tree recursively.
:NvimTreeRefreshRefresh file explorer.

Config File

lua
-- C:\Users\Alowr\AppData\Local\nvim\lua\alowree\plugins\nvim-tree.lua
return {
	"nvim-tree/nvim-tree.lua",
	dependencies = "nvim-tree/nvim-web-devicons",
	config = function()
		local nvimtree = require("nvim-tree")
		-- recommended settings from nvim-tree documentation
		-- disable netrw at the very start of your init.lua
		vim.g.loaded_netrw = 1
		vim.g.loaded_netrwPlugin = 1

		nvimtree.setup({
			view = {
				width = 35,
				relativenumber = true,
			},
			-- change folder arrow icons
			renderer = {
				indent_markers = {
					enable = true,
				},
				-- icons = {
				-- 	glyphs = {
				-- 		folder = {
				-- 			arrow_closed = "", -- arrow when folder is closed
				-- 			arrow_open = "", -- arrow when folder is open
				-- 		},
				-- 	},
				-- },
			},
			-- disable window_picker for
			-- explorer to work well with
			-- window splits
			actions = {
				open_file = {
					window_picker = {
						enable = false,
					},
				},
			},
			filters = {
				custom = { ".DS_Store" },
			},
			git = {
				ignore = false,
			},
		})

		-- set keymaps
		local keymap = vim.keymap -- for conciseness

		keymap.set("n", "<leader>ee", "<cmd>NvimTreeToggle<CR>", { desc = "Toggle file explorer" }) -- toggle file explorer
		keymap.set("n", "<leader>ef", "<cmd>NvimTreeFindFile!<CR>", { desc = "Toggle file explorer on current file" }) -- toggle file explorer on current file
		keymap.set("n", "<leader>ec", "<cmd>NvimTreeCollapse<CR>", { desc = "Collapse file explorer" }) -- collapse file explorer
		keymap.set("n", "<leader>er", "<cmd>NvimTreeRefresh<CR>", { desc = "Refresh file explorer" }) -- refresh file explorer
	end,
}

Config Interpretation

  1. netrw has been disabled or hijacked, therefore command :Explore can no longer be used, or you will receive error message: E492: Not an editor command: Explore.

  2. Note the difference: a. :NvimTreeFindFile almost useless when all files are under the same project tree folder b. :NvimTreeFindFile! reset the tree folder on current file, very useful when managing files under different tree folders

Problem 3

Under certain circumstances, the gf inside Neovim does not work as expected and triggers error message E447: Can't find file "\Users\Alowr\AppData\Local\nvim\lua\alowree\plugins\nvim-tree.lua" in path. This has something to do with the escape of character \.

Rewrite \ inside path with /: C:/Users/Alowr/AppData/Local/nvim/lua/alowree/plugins/nvim-tree.lua, and gf works just fine. Rewrite \ inside path with \\: C:\\Users\\Alowr\\AppData\\Local\\nvim\\lua\\alowree\\plugins\\nvim-tree.lua, and gf works just fine.

By comparison, the gf inside Vim works on all three representations.

See :h gf and :h path.