Migrate from NerdTree to NvimTree

2022-11-13 ⏳2.0 min(0.8k words)

I have used NerdTree as file explorer for years. It is simple, but works. Even after I migrated to NeoVim, I still choose NerdTree to explore and manage file. However, after I upgraded my NeoVim to version 0.8, the NerdTree seems has some bugs under NeoVim. I think it is time to find some modern alternative.

Before the new finding journey, I need to highlight features I mostly used under NerdTree.

Firstly, I like it’s compact UI. Just set g:NERDTreeMinimalUI to 1, and NerdTree only display a file/folder list.

NerdTree’s features

~/.config/nvim/..
⏵ autoload
⏵ ftplugin
⏷ lua
    lv.lua
⏷ pack/vendor/start
  ⏵ tender
  ⏵ tree
  ⏵ vimagit
  LICENSE
  README.md
  init.vim
  vim.lua

NerdTree will display a ⏵ before folder. And you move to some folder and press Enter, the folder will be open and the ⏵ will become ⏷. It so natural. When the folder contains only one folder, NerdTree will open it recursively and merge folders into one line, which is the killer feature I like most. Pressing Enter on file will open it.

If I want to remove, rename or delete some file or folder, I just press m, and NerdTree will pop up a dialog to let me enter the new name or to confirm the action. It is natural as well.

The last feature I used is :NvimTreeFindFile. It will toggle the explorer panel and auto focus to the current file.

I wish the new plugin will have all the features I mentioned.

Hello NvimTree

As I have migrated to NeoVim, it is natural to choose a lua based plugin. And I found the NvimTree.

NvimTree is a more powerful file manager. But it more complicate than NerdTree. When first saw it’s configuration, I can’t help giving up to read them😂It has so many options to setup!

However, the NerdTree’s author seems not to maintain it any more, I have no choice but force myself to understand the setup snippets.

The installation is simple, and just clone NerdTree into pack/*/start:

git clone https://github.com/nvim-tree/nvim-tree.lua.git pack/vendor/start/tree

Then put the following snippet into your init.vim:

vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
vim.opt.termguicolors = true

require("nvim-tree").setup()

Reopen NeoVim and run :NvimTreeToggle will toggle the explorer side panel. And NeoVim also have a :NvimTreeFindFile command to open panel and focus to the path of the current file.

I setup two key map for NvimTree:

nnoremap <silent> <leader>e :NvimTreeToggle<cr>
nnoremap <silent> <leader>f :NvimTreeFindFile<cr>

It works, but the default configuration does not favorite my personal taste. So I need some adjustments.

NvimTree customization

Firstly, NvimTree depends some patched font to display icon. I intend to use plain text UI, so all the non-unicode symbol should be hidden.

There are four kinds of icon will be displayed: git, file, folder, and folder_arrow. And I only need icons of git and folder_arrow.

I use ⏵ and ⏷ for the closed and open arrow, and use ⌥ for the status of git unmerged. All other icon of git status are unicodes. So my configuration for icons is like this:

require("nvim-tree").setup({
  renderer = {
    icons = {
      show = {
        git = true,
	file = false,
	folder = false,
	folder_arrow = true,
      },
      glyphs = {
        folder = {
          arrow_closed = "⏵",
          arrow_open = "⏷",
        },
        git = {
          unstaged = "✗",
          staged = "✓",
          unmerged = "⌥",
          renamed = "➜",
          untracked = "★",
          deleted = "⊖",
          ignored = "◌",
        },
      },
    },
  },
})

NvimTree does not group empty path under the default setup. So we need to set the renderer.group_empty to true.

NvimTree has a killing feature, which is auto change panel width according the length of path. And I like it very much. However, it is not enabled in default. We need open the view.adaptive_size flag. The default width is 30% of the window size, which I think is a little wider. So I change it to 20%.

NvimTree supports opening file by the default application of system. So you can press s and it will let the operating system open the file. But you need to setup the system_open flag in advance. I use mac, so my configuration is:

require("nvim-tree").setup({
  renderer = {
    system_open = {
      cmd = "open",
    },
  },
})

I also open the hijack_cursor flag and set the sort_by to "case_sensitive", please see the help doc to find their functions.

If you want to create, rename, or delete file in NvimTree, you should press a, r, and d accordingly. It is a litter tough to get used to new habit😂

Conclusion

All in all, NvimTree is a good replacement for NerdTree. It has more features, and the project is more active. I think every user of NerdTree shoud consider repace NerdTree with NvimTree.

All the lua configuration can be found in GitHub.