avataralpha2phi

Summary

The web content provides a guide on configuring and using file explorers within Neovim, covering the built-in Netrw browser, the vinegar.vim plugin for enhanced Netrw functionality, and the nvim-tree.lua plugin for a more advanced file explorer interface.

Abstract

The article "Neovim for Beginners — File Explorer" is a comprehensive tutorial aimed at novice users of Neovim, a powerful text editor. It introduces Netrw, Neovim's built-in file browser, detailing its capabilities for local and remote file system navigation. The article also guides readers through configuring Netrw with sensible defaults and enhancing it with the vinegar.vim plugin for a more streamlined project drawer experience. Additionally, it explains how to install and configure the nvim-tree.lua plugin, a Lua-based file explorer offering a tree-style directory view and advanced file manipulation features. The tutorial is part of a broader series on Neovim and includes links to configuration files and installation instructions, as well as references to further learning resources.

Opinions

  • The author suggests that all Neovim users should be familiar with Netrw due to its utility for file and directory operations.
  • Netrw is praised for its support of various protocols, making it versatile for different file transfer needs.
  • The article implies that while Netrw is powerful, configuring it with plugins like vinegar.vim can significantly improve the user experience.
  • nvim-tree.lua is recommended for users seeking a more sophisticated file explorer with a tree-style interface and extensive customization options.
  • The author emphasizes the importance of exploring and manipulating files and directories efficiently within Neovim, highlighting the benefits of a well-configured file explorer setup.

Neovim for Beginners — File Explorer

Configure file explorer for Neovim, with and without plugins.

Neovim for Beginners — File Explorer

Exploring files and directories is what we do daily. Let’s check out how Neovim can help us. We are going to

  • Explore Netrw (:h netrw) which is the built-in file browser supporting local and remote file browsing.
  • Configure sensible defaults for Netrw and check out vinegar.vim that enhances Netrw.
  • Install and configure nvim-tree, a Lua-based plugin for Neovim.

This article is part of the Neovim for Beginners series.

The Neovim configuration files can be found in this repository.

Netrw

All Vimmers should have some basic knowledge of Netrw.

Netrw makes reading files, writing files, browsing over a network, and local browsing easy. It supports many protocols like scp, ftp, http, rsync, sftp, fetch, dav, etc (:h netrw-ref).

We can use it to read a remote file.

nvim scp://hostname/path/to/file
nvim ftp://hostname/path/to/file

We can use it to browse a remote directory.

nvim scp://hostname/
nvim ftp://hostname/path/to/dir/

Local browsing is easy.

nvim .
nvim /home/userid/path

Commands

From within Neovim, we can use the :edit or :e commands to browse the directory. E.g. :e $HOME to browse the home directory, or :e . to browse the current directory.

Other available commands are

  • :[N]Explore[!] [dir]- Explore the directory of the current file.
  • :[N]Hexplore[!] [dir] — Horizontal Split & Explore.
  • :[N]Lexplore[!] [dir] — Left Explorer Toggle.
  • :[N]Sexplore[!] [dir] — Split & Explore the current file’s directory.
  • :[N]Vexplore[!] [dir] — Vertical Split & Explore.
  • :Texplore [dir]— Tab & Explore.
  • :Rexplore — Return to/from the Explorer.
Netrw Explorer

Within Netrw, we can press to see the supported commands for file and directory manipulation, e.g. % to create a file, d to create a folder, R to rename, D to delete, etc.

Configure Netrw

Let’s check out settings we can configure for Netrw (:h netrw-browser-settings).

  • g:netrw:banner — Enable or suppress the banner. 0 to suppress the banner, 1 to enable the banner (default).
  • g:netrw_altv — Change from left splitting to right splitting.
  • g:netrw_browse_split — When browsing, will open the file by 0: re-using the same window (default), 1: horizontally splitting the window first, 2: vertically splitting the window first, 3: open file in a new tab, 4: act like “P” (i.e. open the previous window).
  • g:netrw_liststyle - Set the default listing style- 0: thin listing (one file per line), 1: long listing (one file per line with timestamp information and file size), 2: wide listing (multiple files in columns), 3: tree-style listing.
  • g:netrw_list_hide- Comma-separated pattern list for hiding files.

In the after/plugin/defaults.lua file, add the following lines.

vinegar.vim

If configuring Netrw seems too much hassle, we can use vinegar.vim by tpope. This plugin enhances Netrw to have a project drawer style experience.

In the lua/plugins.lua file, add in the line to install this plugin.

-- Better Netrw
use {"tpope/vim-vinegar"}

Run :PackerInstall to install it.

With the plugin installed,

  • The banner should be disabled.
  • We can press - in any buffer to hop up to the directory listing.
  • Press . on a file to pre-populate it at the end of a : command line.
  • Press y. to yank an absolute path for the file under the cursor.
  • Press ~ to go home.
  • Use Vim’s built-in CTRL-^ (CTRL-6) for switching back to the previous buffer from the netrw buffer.
  • And more fine-tunings…
vinegar.vim

Check out the documentation for more details.

nvim-tree

nvim-tree is a Lua-based file explorer plugin for Neovim.

In the lua/plugins.lua file, add the following lines to install it.

use {
 "kyazdani42/nvim-tree.lua",
 requires = {
   "kyazdani42/nvim-web-devicons",
 },
 cmd = { "NvimTreeToggle", "NvimTreeClose" },
   config = function()
     require("config.nvimtree").setup()
   end,
}

In thelua/config/nvimtree.luafile, add the following lines to configure it.

By default, nvim-tree disables and hijacks Netrw. We can turn it off if required. Check out the documentation for the available options.

In the lua/config/whichkey.lua file, add the key mapping to toggle nvim-tree.

Run :PackerInstall to install it.

Now we should have a proper file explorer to browse our project and manipulate the files and directories.

nvim-tree

To see all the supported commands, press g? to toggle the help.

nvim-tree Help

Check out Learn Neovim The Practical Way for all the Vim/Neovim articles!

If you are not a Medium member yet and want to become one, click here. (A part of your subscription fee will be used to support alpha2phi.)

References

Neovim
Vim
Coding
Programming
Hacking
Recommended from ReadMedium