Better Netrw - Recipes and fixes for Vim's file browser

Published: | 5 min read

A small collection of config settings for netrw. Provided are some great quality-of-life improvements as well as fixes for common netrw issues.

Netrw is vim’s default file browser and can be a very light-weight and minimal solution to browsing files, provided you can live with some of its shortcomings and the work you have to invest into patching it up. Read: you probably don’t want to use netrw.

Luckily, if you still want to use netrw, you don’t have to invest all the effort yourself into getting it up and running! This collection of settings and coding recipes will help you setup a netrw experience that can reasonably compete with other file manager plugins.
Simply choose whatever recipe you need and write it into your own (neo)vim config!

Note that some of these recipes are fixes for the many issues of netrw which maybe eventually patched in newer versions. This guide tries to be relatively up-to-date, but please pay attention to this possibility. Also note that the recipes listed here mostly try to emulate typical IDE sidebar file viewers. If your workflow of using netrw is more “old-school”, some recipes may not apply to you or certain fixes may not even be relevant / break things.

Basics

Making netrw behave like a sidebar file drawer

Basics settings to make netrw work like the “sidebar file viewer” in other popular IDEs. Use the command :Lexplore to toggle the sidebar.

vim.g.netrw_banner = 0 -- Hide the netrw banner on top
vim.g.netrw_altv = 1 -- Create the split of the netrw window to the left
vim.g.netrw_browse_split = 4 -- Open files in previous window. This emulates the typical "drawer" behavior
vim.g.netrw_liststyle = 3 -- Set the styling of the file list to be that of a tree
vim.g.netrw_winsize = 14 -- Set the width of the "drawer"
let g:netrw_banner = 0 " Hide the netrw banner on top
let g:netrw_altv = 1 " Create the split of the netrw window to the left
let g:netrw_browse_split = 4 " Open files in previous window. This emulates the typical 'drawer' behavior
let g:netrw_liststyle = 3 " Set the styling of the file list to be that of a tree
let g:netrw_winsize = 14 " Set the width of the 'drawer'

Setting the file-tree root depending on the opened file

By default, netrw’s tree top / root will always be the directory in which you executed vim in. This snippet makes vim automatically enter the directory of your opened file, thus netrw opens inside that directory as well.

vim.cmd(":cd %:h")
:cd %:h

Custom netrw keybindings

To utilize your own keybindings for netrw, you will have to define them while inside the “netrw filetype”. The best approach for this is to place your keybindings inside the netrw ftplugin:
$VIM_CONFIG/after/ftplugin/netrw.lua or
$VIM_CONFIG/after/ftplugin/netrw.vim

Be aware that some keybindings of netrw are a bit messy and cannot be overwritten. Also make sure to check :map <key> while inside the netrw browser to see what commands are bound to certain keys. You can then reuse these commands in your own bindings as well.

vim.keymap.set("n", "v", "mf", { remap = true, buffer = true }) -- Mark files with v
vim.keymap.set("n", "ll", "<Plug>NetrwLocalBrowseCheck", { noremap = true, buffer = true }) -- Open file/directory with vim direction key
remap <buffer> v mf " Mark files with v
noremap <buffer> ll <Plug>NetrwLocalBrowseCheck " Open file/directory with vim direction key

Adding icons to netrw with netrw.nvim

Using the plugin netrw.nvim, you can have netrw look like modern file browser. The plugin adds devicons for all relevant filetypes making netrw look much more pretty.

Fixes

Remove unnamed ghost buffers when using :Lexplore

This snippet fixes the issue of netrw spawning empty buffers every time you call :Lexplore after you opened a file. With this snippet, use the function toggle_netrw instead of :Lexplore.

Credit for this code goes to david-lorenzo. Transcribed into Lua by me.

-- Remove all empty "No Name" buffers that are unmodified
local function clean_empty_bufs()
	for _, buf in pairs(vim.api.nvim_list_bufs()) do
        if
            vim.api.nvim_buf_get_name(buf) == ""
            and not vim.api.nvim_buf_get_option(buf, "modified")
            and vim.api.nvim_buf_is_loaded(buf)
        then
            vim.api.nvim_buf_delete(buf, {})
        end
	end
end

-- Clean up netrw's empty buffer artifacts and let that logic toggle it
local function toggle_netrw()
	clean_empty_bufs()
	local flag = false
	for _, buf in pairs(vim.api.nvim_list_bufs()) do
        local e, v = pcall(function()
            return vim.api.nvim_buf_get_var(buf, "current_syntax")
        end)
        if
            (e and v == "netrwlist")
            and not vim.api.nvim_buf_get_option(buf, "modified")
            and vim.api.nvim_buf_is_loaded(buf)
        then
            flag = true
            vim.api.nvim_buf_delete(buf, {})
        end
	end

	if not flag then
		vim.cmd(":Lexplore")
	end
end
Check out david-lorenzo's vimscript version at:
https://github.com/david-lorenzo/.dotfiles/blob/main/.vim/explorer.vim

Fix files being created in the wrong directory when using :Lexplore

Using :Lexplore, files created with % are mostly not placed in the directory you expect. This is because netrw does not properly “change” and “enter” into the directory it should for creating the file. To fix this issue, you can overwrite the file creation keybinding % with your own that properly sets the directory.
Doing this introduces a new issue, however, of the newly opened file being now shown inside your netrw-drawer. To fix this undesirable behavior you can further add some commands to your keybinding that will: save the file, close the buffer and reopen netrw.

Credit for this fix goes to u/LinearG on Reddit.

vim.keymap.set("n", "a", "Ccd%:w<CR>:bw<CR>:Lexplore<CR>", { remap = true, buffer = true })
remap <buffer> a Ccd%:w<CR>:bw<CR>:Lexplore<CR>

Make netrw not mess with the clipboard

With the default settings, netrw will try to keep the “clipboard’s values unchanged” which can sometimes lead unexpected issues of not being able to properly copy/paste. To fix this issue, simply turns netrw’s clipboard feature off:

vim.g.netrw_clipboard = 0
let g:netrw_clipboard = 0

Fix subfolders “unloading” when refocusing vim

If you have subfolders expanded inside netrw and somehow refocus vim (i.e. tabbing in and out), netrw will “unload” your subfolders. They will now be displayed as empty text files that you cannot open, requiring you to re-open the parent-folder to make them appear as actual expandable folders again. As a band-aid solution you can use this fix.

vim.g.netrw_fastbrowse = 2
let g:netrw_fastbrowse = 2