Vim Is Much More Powerful Than You Think: Netrw
By Alireza Alavi • 4 minutes read •
Table of Contents
I've been using Neovim for almost 3 years now, and I only used it's built-in Netrw to navigate my project. I always thought Netrw is just a simple file explorer, allowing you to see directories and move between them. That's all. No tree, no copy pasting or moving files, no opening files in other applications.
Man, was I wrong.
Of course I knew about the R key which allows you to rename files/dirs, % which allows you to create new file,D that removes the file/dir and things like that. But no more to be honest. I would just do !thunar .& when things got a bit complicated.
It never amazes me that no matter how much I use and learn about Neovim or Vim, there's still a lot that I don't know.
Today I accidentally pressed i in Netrw and saw that it has a tree view of your directories. The thing I was most missing from Netrw and it was right there!
You can learn everything about it just by pressing <F1> and going through the manual, but here's a quick list.
Cool shortcuts
- Listing/View
- <c-l> Causes Netrw to refresh the directory listing |netrw-ctrl-l|
- i Cycle between thin, long, wide, and tree listings |netrw-i|
- gn Make top of tree the directory below the cursor |netrw-gn|
- I Toggle the displaying of the banner |netrw-I|
- gh Quick hide/unhide of dot-files |netrw-gh|
- Marking
- mf Mark a file |netrw-mf|
- mt Current browsing directory becomes markfile target |netrw-mt|
- mu Unmark all marked files |netrw-mu|
- mc Copy marked files to marked-file target directory |netrw-mc|
- md Apply diff to marked files (up to 3) |netrw-md|
- mg Apply
vimgrepto marked files |netrw-mg| - mr Mark files using a shell-style |regexp| |netrw-mr|
- mv Apply arbitrary vim command to marked files |netrw-mv|
- mx Apply arbitrary shell command to marked files |netrw-mx|
- mz Compress/decompress marked files |netrw-mz|
- p Preview the file |netrw-p|
- CRUD
- d Make a directory |netrw-d|
- D Attempt to remove the file(s)/directory(ies) |netrw-D|
- R Rename the designated file(s)/directory(ies) |netrw-R|
- % Open a new file in netrw's current directory |netrw-%|
- x View file with an associated program (e.g. open HTML file in your browser)
- Navigation
- - Makes Netrw go up one directory |netrw--|
- cd Make browsing directory the current directory |netrw-cd|
- t Enter the file/directory under the cursor in a new tab |netrw-t|
- v Enter the file/directory under the cursor in a new browser window. A vertical split is used.
- Bookmarking
- mb Bookmark current directory |netrw-mb|
- mB Delete bookmark of current directory |netrw-mB|
- qb List bookmarked directories and history |netrw-qb|
{number}gb Go to previous bookmarked directory (or the bookmark number from qb) |netrw-gb|
Remote Files
This is honestly so awesome.
You can just communicate with your remote files from SCP, FTP, SSH, Rsync, curl, dav etc.
I will mostly use this for working on my servers.
If you use SSH keys to authenticate to your server, it will be as simple as
nvim rsync://[user]@[host]/[filepath]
For example:
nvim scp://[user]@[host]//opt/docker-compose-files/
If you want to navigate a dir, add a / at the end of the filepath.
If your SSH key has a password, in your terminal do:
ssh-agent $SHELL
ssh-add // this will ask you pass once
nvim rsync://[host]/[filepath]If you don't have SSH on your destination but have FTP/SFTP do:
nvim sftp://[user]@[host]/path/to/file
NOTE: To be honest I haven't been able to actually work with files in remote sessions with nvim. I haven't gotten much time to troubleshoot it, but I will update with my experiences. If you know how to work with remote file systems, contact me