/post/10 Back

NetRw as a modern file browser

Posted 2026-10-02 06:24:30
vim is an incredible editor, I've been using it exclusivley for many years now. I've always been more of a CLI user rather than a GUI user, Its just so simple, everything is the same size and in my case - everything is restricted to the same colors (ANSI16). There are very few exceptions to my terminal usage, the primary item would be file managers.

My Experience with TUI file managers

Note that this is MY opinion/experience

For quite some time I used the KDE graphical file manager, Dolphin just because it was fast and available but recently I've found myself just using the terminal file operations instead. Using the terminal shell as a file manager works fine, if anything its more extensable than graphical editors because you have the power of scripting at your fingertips. Although this was nice I still found myself yearning for a dedicated manager that would make things simple and more visual than basic cd navigation.

The first program I tried for this was ranger, It was the only TUI file manager I could name off the top of my head. At the time ranger worked fine, it allowed for more intergrated features such as bind-based universal directory sorting and even more useful - multi-media previews. ranger was built to use some semi-familiar vim-style bindings and mode concepts so it did come as a second nature although what I found with ranger (and most other conceptual-vim programs) was that it just dident simulate the vim workspace as much as I wanted - this and the fact that ranger was non-asyncronous and rather laggy whilst navigating around larger directories.

The next program I researched was yazi, an asyncronous rust file manager. yazi worked OK although it seems targeted towards non-traditional linux users as it features fancy nerd-font characters and glyphs that my puny xorg-terminus font could not handle. yazi ended up being even worse than ranger as it had very far-fetched keybind hints and majority of the menu glyphs showed up as unicode font errors even though I had explicitly removed them in the yazi config. yazi was bad, ranger was better but both still missed the vim feeling..

Favorites

My next journey was nnn (n³). nnn is a small C file browser which at face value, trumped both the performance and vim-style of the prior file managers. nnn however was missing in a few features that over time I had become acustomed too, features such as multi-media previews. nnn is very extesable and following the docs I was able to setup a plugin called preview-tui which in a tmux session will open a secondary pane in which a specified program can run on each hovered file. This is inteded for use with image preview commands such as kittyimg or chafa. Because I am using the foot terminal, I ended up intergrating chafa, so now when hovering images I could see a sixel preview in the preview-tui's secondary pane. Now I did end up concluding nnn to a simple wrapper that opens and closes tmux automatically, this may be of use to some people so here is that wrapper:


dwl/config/mkshrc
 2 #
 3 # NNN (N^3) config variables
 4 #
 5 
 6 export NNN_TMPFILE="$HOME/.cache/.nnn_lastd"
 7 export NNN_PREVIEWIMGPROG="chafa -f sixel"
 8 export NNN_PLUG='p:preview-tui'
 9 export NNN_FIFO="/tmp/nnn.fifo"
10 export NNN_PREVIEWDIR="${XDG_CACHE_HOME:-$HOME/.cache}/nnn/previews"
11 
12 # Wrapper that runs TMUX (for preview pane)
13 function n(){
14     tmux new-session "export NNN_TMPFILE='$NNN_TMPFILE'; mksh -c 'nnn -DCHE -P p'" \; set status off
15     if [ -f "$NNN_TMPFILE" ]; then
16         . "$NNN_TMPFILE"
17         rm -f -- "$NNN_TMPFILE" > /dev/null
18     fi
19 }


Anyway, nnn was nice but it too dident have exact vim-like bindings, this wasn't a big issue and I'm just crying cos I'm very picky so moving on.. My next idea was to look into NetRW. NetRW is the editor/explorer built into vim, you can open this with :Ex within vim. Because NetRW is built directly into Vim it has all the same bindings and uses the same config.. What more could you want!! One of the caveats was that I knew that sixel images (Or any terminal-image protocal for that matter) would not work within vim, so embedding a :term window with a preview script would not work. Looking back at nnn, the preview-tui plugin simply uses the tmux multiplexer for secondary pane/window support, and luckily tmux can support sixel images and other related protocals. After some tinkering I came up with the following finished wrapper:


dwl/config/mkshrc
177 # File editor with image previews (built off of netrw)
178 function ex(){
179     tmux new-session "vim -c Ex $1; killall netrw_preview" \; \
180             split-window -h '$DWL_SCRIPT_DIR/netrw_preview' \; \
181             select-pane -t 0 \; \
182             set-window-option -g window-style fg=default,bg=default \; \
183             set-window-option -g window-active-style fg=default,bg=default \; \
184             set status off \; \
185             attach
186     ret=$?
187     cd $(cat /tmp/netrw-current | tail -n 1)
188     return $ret
189 }


Now this simple wrapper does a few things. Firstly it starts a tmux sessing with two panes, the first of which runs NetRW using the vim -c Ex command (Runs :Ex on vim startup). The next split window runs a special script I wrote for generating multi-media previews of the items in Netrw. The rest of that garbage just sets some vanity options for example, making the background of tmux transparent and disabling the bottom-status bar as its not needed.

The custom preview script is rather simple, we just get the current hovered item in NetRw, determine its MIME type, and based on that information we run it though a certain display command. Here is that script:
dwl/script/netrw_preview
 1 #!/bin/sh
 2 #
 3 
 4 # Path stream Location
 5 NETRW_FIFO="/tmp/netrw-current"
 6 
 7 echo "" > "$NETRW_FIFO"
 8 tail -f "$NETRW_FIFO" | while read -r line; do
 9     if [[ -n ${pid:-} ]]; then
10         kill "$pid" 2>/dev/null
11         wait "$pid" 2>/dev/null
12     fi
13 
14     clear
15 
16     # Find files mime type
17     MIME=$(file -b --mime-type "$(readlink -f "$line")")
18     lines=$(tput lines);
19     rlines=$((lines-2))
20 
21     case "$MIME" in
22         *image*)
23             chafa -f sixel "$line" &
24             pid=$!
25         ;;
26         *text*)
27             cat "$line" | head -n $rlines
28             pid=$!
29         ;;
30 
31         *directory*)
32             tree "$line" | head -n $rlines
33             pid=$!
34         ;;
35 
36         *video*)
37             tmp="$(mktemp)"
38             (ffmpegthumbnailer -i "$line" -o "$tmp" && chafa --scale max --symbols vhalf "$tmp" ) &
39             pid=$!
40         ;;
41 
42         *)
43             echo $MIME
44             unset pid
45         ;;
46     esac
47 done

As you can see, the script opens a file labeled NETRW_FIFO, this is a hint as to whats next on the list of intergration schemes. Netrw does not have a native FIFO feature where events are written to a public file. This means that in order to determine the hovered file I had to write some custom Vimscript that would simply write the absolute path of the current HOVERED item (in netrw) to a specific file path. Since Vim is great and the config language is even better this was a rather trivial task. After around a half-hour of reading documentation and coffee breaks I came up with the following:
vim/vimrc
140 " My NETRW config
141 let g:netrw_altv=1
142 let g:netrw_banner = 0
143 let g:netrw_liststyle = 3
144 let g:netrw_altv = 1
145 let g:netrw_winsize = 25
146 let g:netrw_keepdir = 0
147 let g:netrw_keepdir = 1
148 let g:netrw_bufsettings = "nomod nu nobl nowrap"
149 
150 " Enable RNU & NU in NetRW
151 autocmd FileType netrw setlocal number relativenumber
152 
153 " Auto-append the file under the cursor in netrw buffers
154 autocmd FileType netrw
155     \   let b:netrw_last_path = '' |
156     \   autocmd CursorMoved <buffer>
157     \   let word = netrw#Call('NetrwGetWord') |
158     \   if !empty(word) |
159     \       let path = netrw#Call('NetrwFile', word) |
160     \       let path = fnamemodify(simplify(path), ':p') |
161     \       if path !=# get(b:, 'netrw_last_path', '') |
162     \           let b:netrw_last_path = path |
163     \           call writefile([path], '/tmp/netrw-current', 'a') |
164     \       endif |
165     \   endif

The first section as is labeled, is just my own configuration of NetRW which mainly consists of declutering the UI and many depricated config options that either do nothing or don't work (and I guess also do nothing). The next section enables both row numbers and relative row numbers, this aligns with my vim editor setup where I have both the nu and rnu options enabled by default. You of course do not need to copy this but I find it much more easy to navigate using jumps based off of the relative line numbers :). The final part of that snippet (Full config can be found in my vim repo) is the event acccess implementation. The hovered item is captured only whilst in a netrw instance, if there has been a change in the hovered item then it is simply appended to /tmp/netrw-current which is actively listened to by the netrw_preview script. The netrw_preview script is very easy to use, simply add a case for a desired MIME type and add your own scripting into it. I only really use image previews but I can't imagine that it would be too difficult to implement other media types such as PDFs or even animated GIF previews.

NetRW basics

Features I use the most

Sorting by -V

A somewhat tragic compromise with NetRw is that it does not have as many sorting modes as I was like. One very useful mode in particular being a mode that sorts by patterned filenames eg test-0.txt, test-1.txt and test-2.txt. The easiest way I've found to aid this issue would be to use a modifyable buffer and a shell sort on the buffer. This can be done with the following steps:
  1. Enable the buffers modifiable state with :set modifiable
  2. Select the files you want to sort using [V]isual mode
  3. Open the command pallete and enter in !sort -V, (Same behavior as ls -V in the shell)
And that should (After an input prompt or two) now show the files in order.

Marking operations

In NetRw you operate on files though marks. The following binds allow you to use marks efficiently:
Now that files are marked for selection, you need to set a target, this target is used as a destination when copying or moving marked files. You can simply select the current working directory as the target using the mt bind.
Note that if a target destination is not selected, NetRw will prompt for a destination

Now with a target selected you can copy marked files to the target directory using the mc bind, or you can move the marked files using mm.

Running an external program with current file

To do this you have to use NetRw's selection system:
  1. Select a single file target using bind mf
  2. Open eXecution menu with bind mx
  3. In the prompt, enter in the command to run the current targeted file into
I use this method for setting the wallpaper on my computer. On my machine I have a program called dwlbackground that takes in a file path and sets it as the wallpaper, If I followed the previous steps and then entered "dwlbackground" as the execution program - my wallpaper would be set to the marked file.
Note that after a execution, move or copy operation, all marked files will be unmarked. Also note that you can mark files in multiple directories.

Renaming

Press the R bind to rename a single file (At caret location). For multiple/bulk file renaming, mark/select multiple files, then use the same R bind, you will be prompted for each file. If you want to bulk-rename using a pattern then simply enter it on the first file prompt, something a extension swap (s/\.txt$/.md/) can come in useful.
Note that you can move the file by entering a path as the new name. Also note that NetRw sometimes does not automatically refresh after a name change, to refresh just press Ctrl+L

Deletion

Select files and press the D bind to prompt for deletion.

Misc bindings

Bookmarks & History

Searching

Because NetRW is awesome we can use regular visual expressions to search for things. We can search the current open filetree for a certain type of file using :v/*.ext/d and we can search for any phrase by using :v/*/d. To instead remove all of a selection just use the :g command instead of :v

Thanks for listening to this rant.. Now that I've spent at least a days worth of my time focusing on refining my file manager setup I'm going to leave it open and simply admire it..

I'm lazy and so it is unlikley that I will update this post, for more information please look at the following sources:
github.com/ranger/ranger
yazi-rs.github.io/
github.com/jarun/nnn

git.sr.ht/~x3hy/dwl git.sr.ht/~x3hy/vim ++ return 0