Roadside Picnic :::
  • Friends
  • About
  • Categories
  • Tags

>> Home / Posts / Computer

A Farewell to netrw

Navigating through cyberspace

When I first started using vim I was advised to install the NERDtree plugin. As someone lacking familiarity with unix systems, and unable to do much more than cd .. my way around a terminal this was unequivocally good advice. I would like to thank the kind dork who gave it to me and introduced me to the magic of modal editing. Learning to use vim already feels like slamming into a brick wall, taking directory traversal and navigation off the plate of a new user is a mercy and a kindness.

Only after I had a much better grasp of how to edit text in my text editor of choice did I start wondering how to target the window I wanted my file opened in. I don’t believe I ever found a solution I was happy with, but I did find the classic vimcast article Oil and Vinegar. Afterwards I removed NERDtree and forced myself to switch to netrw. For most use-cases netrw is enough, and as the vimcast says it works better in the multi-window split based way that most people prefer to use when editing in vim.

Personally I still find netrw a bit cumbersome, copy and move operations could be more intuitive, and trying to navigate to files that are deeply nested usually ends with me dumping the output of find into a buffer. More importantly I’m stuck in Windows Subsystem for Linux for most of the day, and netrw is painfully slow when navigating into a folder with hundreds of files.

I’ve tried some other vim plugins for file browsing (although not tpope’s vinegar.vim). They all either felt like too much, or would cause an explosion of vimscript error messages when something breaks.

I’ve found a happy alternative that uses the built-in client-server functionality compiled into vim. In tmux I always keep vim open in the first pane, and start it with the following


`vim --servername VIM`

My preferred file manager for general use is vifm which lets you change the command a selected file is open with. In ~/.config/vifm/vifmrc search for line set vicmd and change it to this:


`set vicmd = vim --remote-silent`

Now when you try to open a file in vifm it will instead be sent to your vim running in servermode. I don’t use them but similar changes seem possible for nnn, midnight commander, or other file managers. The --remote-silent flag will let vifm still open your file locally if no vim server is found.

There are some problems with the above method. Anything in your arglist gets wiped and replaced by the file that you just opened, and whatever window your cursor was in will have it’s buffer replaced by the new file as well. This is because the remote send command causes a :drop command to be run, which is what tramples our arglist.

We can spot a partial solution in :help remote


The rest of the command line is taken as the    
file list.  Thus any non-file arguments must    
come before this.    
You cannot edit stdin this way |--|.    
The remote Vim is raised.  If you don't want this use 
>    vim --remote-send "<C-\><C-N>:n filename<CR>"    

That strange preface before the familiar ex command :n is a magic spell that will make the next ex command work regardless of whatever mode your server happens to be in. The command in the manual will bypass the dreaded implicit :drop, but still take over whatever window your cursor is on, if we want a truly ‘silent’ operation we can set a custom vifm hotkey on the R key.

Add the following to your vifmrc:


"send to remote vim 
nnoremap R :!vim --remote-send \"\<C-\\\>\<C-N\>:\$argadd\ %f:p\<CR\>\<CR\>\"<cr>

Let’s break down this tremendous doozy


    "send to remote vim 
    nnoremap R :!

This sets the R key in vifm to run the following in a shell

    `vim --remote-send`

This sends the command to our remote vim instance

    \"\<C-\\\>\<C-N\>:\$argadd\ %f:p\<CR\>\<CR\>"

Almost complete gibberish, lets remove all the escape characters to make it more legible

    "<C-\><C-N>:$argadd %f:p<CR><CR>"

The control characters <C-\><C-N> get us out of any mode we happen to be in. :$argadd is what is going to be run on the vimserver, it adds the file to the end of the arglist, leaving it intact, which also includes it in the buffer list. %f:p is a macro that vifm will expand into the full path of the file selected. <CR><CR> enter key press x2 on the remote vim instance.

    <cr>

This enter press is processed by vifm

Now we're getting somewhere. This is a lot better, but aside from being a nightmare of escape sequences this doesn't work with multiple selected files. A better way would be to put this into a bash script and call that, with some help from wooledge’s BashFAQs I wrote `vifm-remote.sh`:
#!/bin/bash

args=(--remote-send "<C-\><C-N>:\$argadd $* <CR><CR>") 
vim "${args[@]}"

Add the script’s location to your $PATH in your .bashrc


export PATH="/home/redrickSchuhart/Code/bash_scripts:$PATH"

Then Run


source ~/.bashrc

Replace Line in vifmrc


"send to remote vim 
"nnoremap R :!vim --remote-send \"\<C-\\\>\<C-N\>:\$argadd\ %f:p\<CR\>\<CR\>\"<cr>
nnoremap R :!vifm-remote.sh %f:p <cr>

Now any selected files will be sent to your running vimserver and be silently appended to your existing arglist and buffer list, as well as leaving your cursor and window unchanged (with the exception of exiting whatever mode the user is currently in).

This is an odd workflow which still has it’s own drawbacks. Your vim buffers will get rather long with the full paths instead of relative paths listed, but I find it overall to be a much smoother experience.

EDIT: UPDATE PT2


∵ Redrick Schuhart ∴ 2025-11-07

  • Photo
  • Posts

  • Book
  • Computer
  • Sodas

RSS FEED | ATOM FEED | 2025 © Redrick Schuhart | No JavaScript used