How to Use Ranger, the Command Line File Browser

Ranger is a terminal-based file browser with Vim-style keybindings. It uses ncurses and can hook into all sorts of other command line apps to create an incredibly powerful file manager.

If you prefer a graphical experience, more power to you. I’m lazy. Since I’m already using the terminal for 90 percent of what I do, it make sense to not leave it just because I want to browse files.

The keyword here for me is “browse.” I do lots of things to files without using Ranger. Moving, copying, creating, things like that I tend to do directly with cp, mv, touch, mkdir and so on. But sometimes you want browse files, and in those cases Ranger is the best option I’ve used.

That said, Ranger is something of a labyrinth of commands and keeping track of them all can be overwhelming. If I had a dollar for every time I’ve searched “show hidden files in Ranger” I could buy you a couple beers (the answer, fellow searchers, is zh).

I’m going to assume you’re familiar with the basics of movement in Ranger like h, j, k, l, gg, and G. Likewise that you’re comfortable with yy, dd, pp, and other copy, cut, and paste commands. If you’re not, if you’re brand new to ranger, check out the official documentation which has a pretty good overview of how to do all the basic stuff you’ll want to do with a file browser.

Here’s a few less obvious shortcuts I use all the time. Despite some overlap with Vim, I do not find these particularly intuitive, and had a difficult time remembering them at first:

  • zh: toggle hidden files
  • gh: go home (cd ~/)
  • oc: order by create date (newest at top)
  • 7j: jump down seven lines (any number followed by j or k will jump that many lines)
  • 7G: jump to line 7 (like Vim, any number followed by G will jump to that line)
  • .d: show only directories
  • .f: show only files
  • .c: clear any filters (such as either of the previous two commands)

Those are handy, but if you really want to speed up Ranger and bend it to the way you work, the config file is your friend. What follows are a few things I’ve done to tweak Ranger’s config file to make my life easier.

Ranger Power User Recommendations

Enabling line numbers was a revelation for me. Open ~/.config/ranger/rc.conf and search for set line_numbers and change the value to either absolute or relative. The first numbers from the top no matter what, the relative option sets numbers relative to the cursor. I can’t stand relative, but absolute works great for me, YMMV.

Another big leap forward in my Ranger productivity came when I discovered local folder sorting options. As noted above, typing oc changes the sort order within a folder to sort by date created1. While typing oc is pretty easy, there are some folders that I always want sorted by date modified. That’s easily done with Ranger’s setlocal config option.

Here’s a couple lines from my rc.conf file as an example:

setlocal path=~/notes sort mtime
setlocal path=~/notes/reading sort mtime

This means that every time I open ~/notes or ~/notes/reading the files I’ve worked with most recently are at the top (and note that you can also use sort_reverse instead of sort). That puts the most recently edited files right at the top where I can find them.

Having my most recent notes at the top of the pane is great, but what makes it even more useful is having line wrapped file previews so I don’t even need to open the file to read it. To get that I currently use the latest Git version of Ranger which I installed via Arch Linux’s AUR.

This feature, which is invaluable to me since one of my common use cases for Ranger is quickly scanning a bunch of text files, has been merged to master, but not released yet. If you don’t use Arch Linux you can always build from source, or you can wait for the next release which should include an option to line wrap your previews.

Bookmarks

Part of what makes Ranger incredibly fast are bookmarks. With two keystrokes I can jump between folders, move/copy files, and so on.

To set a bookmark, navigate to the directory, then hit m and whatever letter you want to serve as the bookmark. Once you’ve bookmarked it, type `<letter> to jump straight to that directory. I try to use Vim-like mnemonics for my bookmarks, e.g. `d takes me to documents, `n takes me to ~/notes, `l takes me to the dev folder for this site, and so on. As with the other commands, typing just ` will bring up a list of your bookmarks.

Conclusion

Ranger is incredibly powerful and almost infinitely customizable. In fact I don’t think I really appreciated how customizable it was until I wrote this and dug a little deeper into all the ways you can map shell scripts to one or two character shortcuts. It can end up being a lot to keep track of though. I suggest learning maybe one or two new shortcuts a week. When you know longer have to think abut them, move on to the next couple.

Or you can do what I do, wait until you have something you want to do, but don’t know how, figure out how to do it, then write it down so you remember it.

Shoulders Stood Upon


  1. In fact, just type o and you’ll get a list of other sorting options (and if you know what normal means, drop me a comment below, I’m still trying to figure out what that means).