Configure Nvim with Lua: File Explorer (5)

3 min read, Published on Sep 25, 2022

Vim/Neovim has a file explorer that we can use to move within the folders of our project, or simply the directory that we want to work. If you open a terminal and start Neovim, you can run this command:

  :Lexplore

According to the documentation: With Lexplore toggles a full height Explorer window on the left hand side of the current tab. It will open a netrw window on the current directory if [dir] is omitted; a :Lexplore [dir] will show the specified directory in the left-hand side browser display no matter from which window the command is issued.

If you want to change the size you can do this:

  :Lexplore 25

You can see that now it has a size similar to VS Code file explorer. With this command you can open and close the file explorer, but if you wish, you can create a custom keymap to facilitate this. Example:

  1. We move to the nvim folder:
  cd .config/nvim
  1. Then, we will open the keymaps file.
  nvim lua/configs/keymaps.lua
  1. We add this code and save it:
  -- ...others configs...
  -- toggle file explorer
  keymap('n', '<S-e>', ':Lexplore 25<cr>')
  -- ...others configs...
  • Now you can press Shift + e combination to toggle file explorer.

You already have your keymap configured for the vim/neovim file explorer. Also, there are some plugins (some of them are only for neovim) that add more features as a file explorer:

In this case, we going to test nvim-tree.lua, because it is written in Lua. But, you can experiment with the others, and use the one you wish.

Use nvim-tree.lua

  1. The first step: we add the plugin nvim-tree.lua to the plugins.lua file (where we are going to put the plugins to install).
  nvim lua/configs/plugins.lua
  1. We add this and save it:
  -- ...others configs...
  return packer.startup(function(use)
    -- ...others plugins...

    use {
      'kyazdani42/nvim-tree.lua',
      requires = {
        'kyazdani42/nvim-web-devicons', -- optional, for file icons
      },
      tag = 'nightly' -- optional, updated every week.
    }

    -- ...others configs...
  end)
  1. Install the packages:
  :PackerInstall
  1. Later, we create the nvim-tree file.
  nvim lua/configs/nvim-tree.lua
  1. We add this code and save it:
  local ok, nvim_tree = pcall(require, "nvim-tree")
  if not ok then
    return
  end

  -- disable netrw at the very start of your init.lua (strongly advised)
  vim.g.loaded = 1
  vim.g.loaded_netrwPlugin = 1

  nvim_tree.setup({
    view = {
      width = 35
    },
    renderer = {
      icons = {
        glyphs = {
          git = {
            unstaged = "",
            untracked = "U",
          },
        },
      }
    },
    update_focused_file = {
      enable = true,
      update_cwd = true,
      ignore_list = {},
    }
  })
  1. Open the keymaps.lua file (where we are going to put the custom keymaps).
  nvim lua/configs/keymaps.lua
  1. Add this:
  -- ...others keymaps...

  -- nvim-tree toggle (open/close with Shift + e)
  keymap('n', '<S-e>', ':NvimTreeToggle<cr>', opts)
  -- change of window (example: when you are open nvim-tree, or you are splitted the window)
  keymap('n', '<C-h>', '<C-w>h', opts)
  keymap('n', '<C-j>', '<C-w>j', opts)
  keymap('n', '<C-k>', '<C-w>k', opts)
  keymap('n', '<C-l>', '<C-w>l', opts)

  -- ...others keymaps...
  1. To use these settings, we need to call the nvim-tree file in the main file (init.lua)
  nvim init.lua
  • Add this:
  require('configs.nvim-tree')

And that’s it!! You can use nvim-tree!

Directory

We have the directory of folders like this:

nvim
├─ init.lua
└─ lua
    └─ configs
        └─ options
        |   └─ init.lua
        └─ keymaps.lua
        └─ plugins.lua
        └─ impatient.lua
        └─ colorscheme.lua
        └─ nvim-tree.lua

Conclusion

In this article we learned how to use vim/neovim file explorer. Also, there are plugins that we can use. In this case we install and configure nvim-tree.lua. Now you can test other file explorer if you wish. I hope it will be helpful for those who want to experiment.

Resources