Programming

Go to back directory browsing after opening file in vim

25 September 2026 · 8 min read

Go to back directory browsing after opening file in vim

Navigating project directories efficiently is a cornerstone of productive coding. For Vim users, the transition from editing a specific file back to a comprehensive directory view can sometimes feel like a slight detour, especially when you’re deep into a complex project structure. The ability to seamlessly go to back directory browsing after opening file in Vim is not just a convenience; it’s a critical skill that enhances workflow, reduces cognitive load, and keeps you focused on your code. This guide will demystify the various methods available in Vim, from its built-in file explorer, netrw, to powerful plugins and advanced buffer management techniques, ensuring you can always find your way back to your directory tree with ease.

Mastering Vim’s Built-in File Explorer: netrw

Vim’s default file explorer, known as netrw, is a powerful yet often underestimated tool for directory navigation. When you open Vim without specifying a file (e.g., vim .), or use commands like :Ex or :Vex, you’re interacting with netrw. Understanding its core functionalities is the first step to efficiently manage your file system within Vim. Netrw allows you to browse directories, open files, create new files or directories, and even delete them, all from within your editor window. Its strength lies in its ubiquity – it’s always there, requiring no extra installation, making it a reliable companion for any Vim user.

When you open a file from a netrw buffer, Vim essentially replaces the directory listing with the content of your chosen file. The challenge then becomes how to revert to that directory listing without closing your current file or opening a new instance of netrw. Seasoned Vim users often preach the philosophy of minimizing external tools, and netrw fits this perfectly by providing robust Vim directory navigation capabilities. According to Drew Neil, author of “Practical Vim,” mastering built-in features like netrw is crucial for a truly efficient Vim workflow, as it fosters muscle memory and reduces reliance on external configurations that might not always be available.

One of the key reasons developers appreciate netrw is its simplicity and integration. While it might not have the visual flair of some GUI-based file explorers, its text-based interface is incredibly fast and responsive, especially over SSH connections or on resource-constrained systems. It supports various commands for listing files, changing directories, and even basic remote file access. To truly leverage netrw, one must become familiar with its keybindings, which, while initially daunting, open up a world of efficient file management in Vim directly from the command line.

Seamlessly Returning to Directory View with netrw Commands

Once you’ve opened a file from a netrw buffer, getting back to that directory listing is simpler than you might think. Vim provides specific commands to help you navigate your file history and window layouts. The most straightforward way to return to the previous directory browsing view after opening a file is by using the special buffer for netrw. When you open a file from netrw, netrw’s buffer doesn’t disappear; it simply gets pushed into the background or becomes inactive in the current window.

The command :Ex or :Vex (for horizontal or vertical splits) can be used to open netrw, but if you’re already in a file that was opened from netrw, you might want to switch back to the existing netrw buffer. The command :cd %:h combined with :Ex can take you to the directory of the current file and then open netrw. However, a more elegant solution for Vim directory navigation is often to use the ^ (Ctrl-^) key combination or the :bprevious command if you’ve been moving between buffers. Alternatively, if netrw was in a split window, you can use window navigation commands like p (go to previous window) or h/j/k/l to move between splits.

The most efficient method to go to back directory browsing after opening file in Vim is often through the use of the :Explore command. When you execute :Explore (or its variants like :Hexplore, :Vexplore, :Sexplore), Vim opens netrw in the current window or a new split, showing the contents of the current working directory. If you’ve opened a file from a netrw buffer and now wish to return to that exact previous directory listing, simply typing :Explore will often achieve this, provided your current working directory hasn’t changed. This command is particularly useful for quickly jumping back into a visual representation of your project’s structure, allowing you to select another file or explore sibling directories.

Another powerful technique involves the use of Vim’s buffer list. When you open a file from netrw, the netrw buffer remains in your buffer list. You can view all open buffers with :ls and then switch to the netrw buffer by its number using :b <buffer_number>. This method offers precise control, especially when you have multiple netrw instances open in different directories or complex buffer management scenarios.</buffer_number>

Enhancing Navigation with External Plugins: NERDTree and Beyond

While netrw is powerful, many Vim users opt for external plugins to augment their Vim file explorer experience, offering features like persistent tree views, bookmarks, and more intuitive navigation. Plugins like NERDTree, Vim-Dirvish, and coc-explorer provide a richer, more visual way to browse your file system and can streamline the process of returning to a directory view.

NERDTree is arguably the most popular file explorer plugin for Vim. It provides a persistent, tree-like directory structure in a sidebar, making it incredibly easy to see your project’s layout at a glance. When you open a file from NERDTree, the file opens in the main editing window, and NERDTree remains open in its dedicated sidebar. To navigate back to the directory listing (i.e., interact with NERDTree again), you simply need to move your cursor back to the NERDTree window. This can be done with standard window navigation commands like h (if NERDTree is on the left) or by using NERDTree’s own toggling command to close and reopen it, which often jumps your cursor back to it. This approach significantly simplifies the task of going back to directory browsing after opening a file in Vim, as the directory view is always present.

Here’s how to effectively use NERDTree for directory browsing:

  1. Install NERDTree: Use a plugin manager like Vim-Plug, pathogen, or Vundle. For Vim-Plug, add Plug ‘preservim/nerdtree’ to your .vimrc and run :PlugInstall.
  2. Toggle NERDTree: Use :NERDTreeToggle to open or close the file explorer. Many users map this to a shortcut, like map :NERDTreeToggle.
  3. Open Files: Navigate the tree with j, k, h, l. Press Enter on a file to open it in the main window.
  4. Return to NERDTree: If your cursor is in the file buffer, use h (or the appropriate window navigation command) to move back to the NERDTree window and continue browsing.

This persistent sidebar approach is a game-changer for many, providing continuous context of the project structure. Other plugins like vim-dirvish offer similar functionalities, often with different philosophies or keybindings. The choice between netrw and a plugin like NERDTree often comes down to personal preference for visual representation and persistent views versus the lean, command-line driven approach.

Advanced Techniques: Buffer Management and Window Splits

Beyond explicit file explorers, Vim’s robust buffer and window management capabilities offer powerful ways to handle file and directory navigation. Thinking of your workflow in terms of buffers – each file, and even each netrw instance, being a buffer – allows for incredible flexibility. When you open a file from a directory listing, Vim simply switches the active buffer in that window. To go to back directory browsing after opening file in Vim, you can often just switch back to the buffer that contains your directory listing.

  • Buffer Cycling: Use :bprevious or :bnext to cycle through your open buffers. If your netrw buffer is still in the buffer list, you’ll eventually cycle back to it. For more direct access, :ls shows all buffers with their numbers, allowing you to jump to a specific netrw buffer with :b <buffer_number>. This method is highly effective for managing multiple files and directory views without constantly reopening explorers.</buffer_number>

  • Window Splitting: Instead of opening files Question & Answer :
    When I open a directory browsing in vim, I can open a file by moving the cursor to a file name and pressing Enter.

    Now, is there any command to go back to the previous directory browsing?

    If not, is there a way to open a file without closing the directory browsing mode?

    You can go back to the last buffer using :b#.

    If you just opened a file, then it will bring you just back to the directory browsing.

    Update: Since this answer happened to be accept as the correct answer and is thus on the top, I’d like to summarize a bit the answers, including the one by @romainl that imho is the correct one.