🗃️ File Explorer for Neovim

Shaik Zahid
6 min readFeb 7, 2023

--

Nvimtree is a Neovim plugin to browse the file system and other tree like structures in whatever style suits you, including sidebars, floating windows, split style, file icons or all of them at once!

Installation

  • Add the github repository reference to our plugins.lua file in lua directory.
-- plugins.lua 

return {

-- Bufferline
{
'akinsho/bufferline.nvim',
dependencies = {
'nvim-tree/nvim-web-devicons'
},
},

-- Colorscheme
{
'folke/tokyonight.nvim',
},

-- Hop (Better Navigation)
{
"phaazon/hop.nvim",
lazy = true,
},


-- Lualine
{
'nvim-lualine/lualine.nvim',
dependencies = {
'nvim-tree/nvim-web-devicons'
},
},

-- Nvimtree (File Explorer)
-- Added this reference to the initial file
{
'nvim-tree/nvim-tree.lua',
lazy = true,
dependencies = {
'nvim-tree/nvim-web-devicons',
},
},

-- Which-key
{
'folke/which-key.nvim',
lazy = true,
},

}
  • The lazy keyword enables lazy-loading of nvim-tree plugin. This will only load it when you want to search inside the editor.
  • Now invoke Lazy(Package Manager) using Space + p key in Normal mode to install and load.
  • Install nvim-tree using “shift + i” and we can go on to the next steps of configuring the file explorer.

File Explorer Configuration

  • Create a new file nvim-tree-config.lua in your lua directory.
  • Configure nvim-tree file using the documentation. My configuration looks like this…
-- nvim-tree-config.lua

local status_ok, nvim_tree = pcall(require, "nvim-tree")
if not status_ok then
return
end

local config_status_ok, nvim_tree_config = pcall(require, "nvim-tree.config")
if not config_status_ok then
return
end

local tree_cb = nvim_tree_config.nvim_tree_callback

nvim_tree.setup {
update_focused_file = {
enable = true,
update_cwd = true,
},
renderer = {
root_folder_modifier = ":t",
-- These icons are visible when you install web-devicons
icons = {
glyphs = {
default = "",
symlink = "",
folder = {
arrow_open = "",
arrow_closed = "",
default = "",
open = "",
empty = "",
empty_open = "",
symlink = "",
symlink_open = "",
},
git = {
unstaged = "",
staged = "S",
unmerged = "",
renamed = "➜",
untracked = "U",
deleted = "",
ignored = "◌",
},
},
},
},
diagnostics = {
enable = true,
show_on_dirs = true,
icons = {
hint = "",
info = "",
warning = "",
error = "",
},
},
view = {
width = 30,
side = "left",
mappings = {
list = {
{ key = { "l", "<CR>", "o" }, cb = tree_cb "edit" },
{ key = "h", cb = tree_cb "close_node" },
{ key = "v", cb = tree_cb "vsplit" },
},
},
},
}

Source this code to main configuration

  • The init.lua file looks like this….
require "keymaps"
require "options"
require "lazy-config"
require "hop-config"
require "lualine-config"
require "nvim-tree-config" -- Added this line to initial file
require "bufferline-config"
require "whichkey"

Add Keybindings to Which-key

  • Here we will configure Space + e key to toggle the file explorer.
  • Add this code in the mappings variable in whichkey.lua file.
    ["e"] = { "<cmd>NvimTreeToggle<cr>", "Explorer" },
  • The updated whichkey.lua file will look like this…
-- Whichkey.lua 

local status_ok, which_key = pcall(require, "which-key")
if not status_ok then
return
end

local setup = {
plugins = {
marks = true, -- shows a list of your marks on ' and `
registers = true, -- shows your registers on " in NORMAL or <C-r> in INSERT mode
spelling = {
enabled = true, -- enabling this will show WhichKey when pressing z= to select spelling suggestions
suggestions = 20, -- how many suggestions should be shown in the list?
},
-- the presets plugin, adds help for a bunch of default keybindings in Neovim
-- No actual key bindings are created
presets = {
operators = false, -- adds help for operators like d, y, ... and registers them for motion / text object completion
motions = true, -- adds help for motions
text_objects = true, -- help for text objects triggered after entering an operator
windows = true, -- default bindings on <c-w>
nav = true, -- misc bindings to work with windows
z = true, -- bindings for folds, spelling and others prefixed with z
g = true, -- bindings for prefixed with g
},
},
-- add operators that will trigger motion and text object completion
-- to enable all native operators, set the preset / operators plugin above
-- operators = { gc = "Comments" },
key_labels = {
-- override the label used to display some keys. It doesn't effect WK in any other way.
-- For example:
-- ["<space>"] = "SPC",
-- ["<cr>"] = "RET",
-- ["<tab>"] = "TAB",
},
icons = {
breadcrumb = "»", -- symbol used in the command line area that shows your active key combo
separator = "➜", -- symbol used between a key and it's label
group = "+", -- symbol prepended to a group
},
popup_mappings = {
scroll_down = "<c-d>", -- binding to scroll down inside the popup
scroll_up = "<c-u>", -- binding to scroll up inside the popup
},
window = {
border = "rounded", -- none, single, double, shadow
position = "bottom", -- bottom, top
margin = { 1, 0, 1, 0 }, -- extra window margin [top, right, bottom, left]
padding = { 2, 2, 2, 2 }, -- extra window padding [top, right, bottom, left]
winblend = 0,
},
layout = {
height = { min = 4, max = 25 }, -- min and max height of the columns
width = { min = 20, max = 50 }, -- min and max width of the columns
spacing = 3, -- spacing between columns
align = "left", -- align columns left, center or right
},
ignore_missing = true, -- enable this to hide mappings for which you didn't specify a label
hidden = { "<silent>", "<cmd>", "<Cmd>", "<CR>", "call", "lua", "^:", "^ " }, -- hide mapping boilerplate
show_help = true, -- show help message on the command line when the popup is visible
triggers = "auto", -- automatically setup triggers
-- triggers = {"<leader>"} -- or specify a list manually
triggers_blacklist = {
-- list of mode / prefixes that should never be hooked by WhichKey
-- this is mostly relevant for key maps that start with a native binding
-- most people should not need to change this
i = { "j", "k" },
v = { "j", "k" },
},
}

local opts = {
mode = "n", -- NORMAL mode
prefix = "<leader>",
buffer = nil, -- Global mappings. Specify a buffer number for buffer local mappings
silent = true, -- use `silent` when creating keymaps
noremap = true, -- use `noremap` when creating keymaps
nowait = true, -- use `nowait` when creating keymaps
}

local mappings = {

["k"] = { "<cmd>bdelete<CR>", "Kill Buffer" }, -- Close current file
["p"] = { "<cmd>Lazy<CR>", "Plugin Manager" }, -- Invoking plugin manager
["q"] = { "<cmd>wqall!<CR>", "Quit" }, -- Quit Neovim after saving the file
["w"] = { "<cmd>w!<CR>", "Save" }, -- Save current file

-- Added this line from the initial file
["e"] = { "<cmd>NvimTreeToggle<cr>", "Explorer" }, -- File explorer

}

which_key.setup(setup)
which_key.register(mappings, opts)
  • Which-key plugin will look like this…

Keybindings of File Explorer

  • Space + e :- Toggle File Explorer
  • a :- Add a new file
  • d :- Remove a file
  • o :- Open a file and stay in file explorer
  • j :- Move down
  • k :- Move up
  • l :- Open file and move to editor
  • h :- Minimize folder
  • ctrl + l :- Jump to editor
  • ctrl + h :- Jump to file explorer

Nvim-Tree Alternatives

Neovim From Scratch

  • If you want a complete installation and configuration of Neovim from Scratch, then you can head over to my NEOVIM SERIES.
  • This series is updated regularly, with updates and inclusion of newer plugins which improves the wholesome IDE experience of Neovim.

--

--