Netrw in Vim: A Comprehensive Guide for Beginners

Basic Usage of Netrw

Opening Netrw

How to Open Netrw in Vim

To open Netrw within Vim, you can use several commands depending on how you want to view your directories and files:

  • Explore: Type :Explore to open Netrw in the current window. This replaces your current buffer with a file explorer view.
  • Horizontal Split: Type :Sexplore to open Netrw in a horizontal split window. This allows you to view files and directories in the lower half of the screen while keeping your current file visible.
  • Vertical Split: Type :Vexplore to open Netrw in a vertical split window. This splits the screen side by side, with Netrw on one side.

Netrw lets you navigate directories using straightforward Vim commands:

  • Enter a Directory: Position the cursor over the directory name and press Enter to navigate into the directory.
  • Go Up a Directory: To move up one level, position the cursor on the line that represents the parent directory (usually denoted by ../) and press Enter.

Opening Files

Opening Files from Netrw

When you find the file you want to edit:

  • Open in the Current Window: Press Enter on the selected file to open it in the current window.
  • Open in a New Tab: Press t on the selected file to open it in a new tab, allowing you to keep multiple files open simultaneously.
  • Open in a Split Window: Use s to open a file in a horizontal split window or v for a vertical split window, directly from Netrw.

Closing Netrw

Closing Netrw and Returning to Vim

To exit Netrw and return to your regular Vim environment:

  • Closing a Single Netrw Window: If Netrw is opened in a split window, you can simply close it like any other Vim window by typing :q.
  • Returning to a Previous Buffer: If you opened Netrw in the current window, press Ctrl-^ (Control and caret) to switch back to your last buffer, or use :b# to go back to the previous buffer.