vim

Managing Files using Vim File Explorer

Vim has a default file explorer called Netrw which can be accessed using the :Explore command to explore the file system.

If you want to find and open a file right from the Vim window, then you can do it using Vim file explorer. File explorer also known as file browser is used to manage and open the files.

The Vim file explorer is a buffer (a temporary memory space for the opened file) that lists the directories instead of showing the content of the files. In this tutorial, I will be exploring the Vim file explorer, and how to navigate it and open a file.

Note: In this tutorial, I am using the Vim editor version 8.2 in Linux (Ubuntu 22.04); a few commands may differ from operating system to operating system (macOS/Windows).

Open Vim File Explorer

To open the file explorer in Vim, execute the :Explore command.

:Explore

It will list the directory of the currently opened file. It can also explore the directories of a path if mentioned after the command. For example:

:Explore /home/sam

If we open the Vim editor by giving it a path instead of the file name, then it will open the default Vim file explorer. For example:

vim /home/sam

Or:

vim .

There are various approaches to open the Vim file explorer, some are listed below:

Hexplore \ Hexplore! Horizontal splitting below \ above | Open the file in the explorer window
Lexplore \ Lexplore! Vertical splitting left \ right | Open the file in the opposite window
Texplore Open explorer in a tab | Open file in a new tab

Using Vim File Explorer

Some key operations that can be performed with the default Vim file explorer are listed below:

To navigate the Vim file explorer, use the directional or h, j, k, and l keys.

  • To enter a directory, press the Enter key, and to move up in the directory use the dash (-) key.
  • Use the % key to create a new file and d to create a new directory.
  • Press Enter to explore a directory or open a file under the cursor.
  • Press the D key to delete a file or directory, and then choose from the four options yes, no, all, and quit.
  • To rename the file, press the R key and then rename the file from the status bar.
  • To sort the file, press the s key which will toggle the sorting by name, size, extension, and time.
  • To preview the file under the cursor, press the p key and then Enter.
  • Press v to enter the current director and explore its content in a new window.
  • Use the x to view the file in the corresponding application. For example, the text files will be opened in the text editor and the DOCX file will be opened in MS Word.
  • To exit the file explorer by using the :q command.

The file explorer becomes even more useful if you enable the mouse functionality. To set the mouse functionality in Vim, use :set mouse=a command. With the mouse click you can open files, explore directories, and even resize the windows.

To learn more about Vim file explorer, type :help Explore in the Vim editor.

Other Methods to Use the Vim File Explorer

There are some other approaches as well to use the Vim file explorer. For example, to open the directory in the Vim file explorer, use the :e command with path.

:e <path>

To display the content of the current working directory, press :e and then ctrl+d.

You can also use :e with the characters of the file name, and then press the tab key to toggle between all the files that contain those characters.

For example, if I use :e fi and press the tab key repeatedly, it will toggle all the files that contain the fi characters.

Using the Third-Party File Explorer for Vim

Vim is not limited to the default Netrw file explorer; you can enhance your Vim experience using plugins. Apart from the default Netrw file explorer, Vim gives the freedom to use another file explorer as well. A list of some popular file explorers is:

I am going to install the NERDTree file explorer plugin through the VimPlug plugin manager.

Use the following command to install the VimPlug plugin manager:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Place the following lines in the vimrc file to install the NERDTree file explorer.

call plug#begin()

Plug 'preservim/nerdtree'

call plug#end()

Open the Vim editor and type the :PlugInstall command; this will begin the downloading the installation process of the plugin.

To open the NERDTree file explorer in Vim use :NERDTree command.

:NERDTree

Similarly, you can install and explore other Vim file explorers.

Conclusion

Vim file explorer is another hidden feature that can let you explore directories and files without leaving the Vim window. You can manage files and directories directly from Vim’s built-in file explorer called Netrw. The Netrw file explorer offers a comprehensive range of options to facilitate the access and management of files. But you can also explore other file explorers to enhance the Vim experience, such as NERDTree or fzf.vim plugins.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.