Vim File Navigation and Bookmarking with NETRW
When I first started using Vim I read a lot about how I should install a plugin called NerdTree to enable a project drawer type file browser in Vim. Whilst NerdTree is indeed great, after using splits in Vim constantly and reading this article by Drew Neil, I realised that NerdTree wasn't working for me. The problem is if, like me, you use Vim split views, opening a project drawer to one side of these splits means there's no visual relationship as to which split a chosen file should open in.
At that point I decided to move to the built-in (though actually a plug-in) file browser called NETRW. I've been using it for several months now and has become my default way to manage files in Vim. I've also recently employed a nice bookmarking system for directories but more on that in a moment.
What About Fuzzy File Finding?
I just want to make a small note here that when I want to open a file in Vim I use the amazing FZF and associated Vim plugin so I can do a fuzzy search rather than browsing a directory. It's incredibly fast and can fuzzy find across files, your file history, your open buffers, tracked git files and even on every line you have open in all open buffers. Sometimes though I need to create directories and the like or just look at a directory structure and that's when I use NETRW.
In the Vim Vinegar article, you'll see how NETRW works much better as a file browser when working with splits and buffers when you think about the idea of a buffer flipping around to reveal the file structure on the other side.
To trigger NETRW I've mapped CTRL-n to edit . like this:
map <c-n> :edit .<CR>
This will open the NETRW browser for the current directory of the buffer I'm currently editing. The main commands I then use day-to-day are:
d — create directory
% – create file
D — delete directory
R — rename directory
^ — jump back to the previous buffer
Together with the j and k keys to move up and down, RETURN to enter a directory and - to go back up one directory.
Because NETRW is a buffer you can use the usual commands to jump around as well such as / for search, gg for jumping to the top of the list and G for going to bottom of the list.
You can also hide certain file types too. By default I hide a lot of image and movie file types as they're generally not relevant for Vim. To do this I add this line in my .vimrc
let g:netrw_list_hide= '.*\.png$,.*\.pdf,.*\.mp4,.*\.tga,.*\.mp3,.*\.jpg,.*\.svg,/*\.stl,.*\.mtl,.*\.ply'
If I want to see these files then I can simply press a to flip through the different display modes.
NETRW has a built in bookmarking system, but to be honest it's not great. You can press mb to add a directory as a bookmark, press qb to get a list of those bookmarks and then press 1gb for instance to go to bookmark 1 in the list. Yet there doesn't seem to be any way to edit a bookmark and adding new bookmarks shifts the numbers of where these bookmarks are stored. Pretty pointless beyond rudimentary usage.
Better Directory Bookmarking with Quickmenu
I wanted a system were I could bring up a list of bookmarks that I've defined and use that to jump around the file system. After some searching I found the really nice Quickmenu plug-in.
Using Quickmenu I can define commands which appear in a menu to the side of the current buffer. These commands can be anything, except in this case I'm using it — at the moment at least — to be a way to bookmark favorite directories.
After installing the plug-in and binding F12 to the Quickmenu toggle I then setup the directories I needed in my .vimrc like this:
call g:quickmenu#append('# Directories', '') call g:quickmenu#append('Dropbox', 'edit ~/Dropbox | normal c', 'Go to Dropbox Directory') call g:quickmenu#append('brendandawes.com', 'edit ~/Dropbox/Sites/brendandawes.com\ grid\ pro/html | normal c','Go to brendandawes.com Directory') call g:quickmenu#append('blogs', 'edit ~/Dropbox/Sites/brendandawes.com\ grid\ pro/html/content/05-blog | normal c','Go to brendandawes.com Blog Directory') call g:quickmenu#append('processing', 'edit ~/Dropbox/Processing | normal c','Go to brendandawes.com Processing Directory')
Notice the | normal c part of the append line. This is so when it triggers NETRW to appear for that directory it also sets the current directory to the same path (this is what the c key does in NETRW). That means I can then — if I want to — use FZF to fuzzy search from that directory downwards, which comes in really handy.
I'm not really one for using lots of plug-ins in my Vim configuration, and if I do they have to be ones I use everyday. I feel that Quickmenu when combined with NETRW gives a nice solution to bookmarking favorite directories.