File Management: ranger

We’ve been talking for a while about file management in Vim. Let’s take some time to talk about file management outside of Vim. In particular, I want to introduce a program called ranger.

ranger is a console-based file manager. You launch it by simply typing ranger in the console and it takes over, filling the current screen or window, offering you a visual view of the file system from the location you launched it.

You can install ranger through apt on Debian/Ubuntu or homebrew on Mac, and I’m sure it’s easy to find in package managers for other *nix distros.

ranger is a paned file manager similar to one of the modes in Mac’s Finder. You are always focused on the center pane. Moving right takes you into folders, moving left brings you back up the tree. If you are focused on a file, it will try to show a preview of that file in the right pane.

If the file is text based, the preview is easy enough. Otherwise, it generally skips the preview. Though there are ways to display image previews, either with ascii image plugins or other terminal specific hacks and third party program integrations. I haven’t bothered.

One of the great things about ranger (if you’re a Vim user) is that it uses Vim key bindings for folder navigation. k and j move up and down through files in the current directory. l moves right to go into a subdirectory and h goes left to go up to the parent directory.

You can also use gg and G go to the top and bottom of the file list in the current directory or / to search within that list.

To move a file, highlight it and press dd, navigate to where you want it to go and press pp. Use yy to copy a file instead of moving it. Note however that dd does not actually delete the file until you do pp to paste it. If you simply want to delete a file, use dD instead.

So yeah, the shortcuts in general are more Vim-like than strictly Vim, and will take a little learning. However, a lot of them are two key combos, and when you press the first key, a menu will pop up showing you a listing of what you can type next.

Renaming a file is a bit non-intuitive. Press a. I guess you can think of it as “appending” to the file name.

The g key gives you a “go” menu, with shortcuts to take you to common places on your file system. Super useful. Eventually you’ll start learning the more common ones by heart. gh = “go home”, i.e. cd ~.

There’s also a visual mode for selecting multiple files and performing actions on them. Even though I’m just skimming over stuff here, this is going to be a long “tip”. ranger is just that powerful.

Beyond managing and previewing files, you eventually probably want to open them in some program for viewing, editing or what have you. Just hit enter on a selected file to do that. But how does ranger know what program to use to open that file? Interestingly, it does not just rely on system associations for this functionality. It has its own internal system known as “rifle” that determines a file’s type and what programs exist on the system that can handle that type. This is all configured through a rifle.conf file.

Now manually crafting your own rifle.conf would be tedious, but ranger comes with a default one. You’ll probably need to tweak it a bit depending on what system you are on, what programs you have installed, and which programs you want to open for different file types. But that default config is hidden away in an application directory you don’t really want to touch. To make a copy of it, run:

1
ranger --copy-config=rifle

This will copy the default rifle.conf to ~/.config/ranger/rifle.conf where you can safely edit it, back it up, and share it among your different machines.

The rifle.conf file works by identifying the file type and matching it with a program on the system. For various types of text files, here’s what it has:

1
2
3
4
5
# Define the "editor" for text files as first action
mime ^text, label editor = $EDITOR -- "$@"
mime ^text, label pager = "$PAGER" -- "$@"
!mime ^text, label editor, ext xml|json|csv|tex|py|pl|rb|js|sh|php = $EDITOR -- "$@"
!mime ^text, label pager, ext xml|json|csv|tex|py|pl|rb|js|sh|php = "$PAGER" -- "$@"

Here, if the mime type of the file shows that it is text, it will use the program defined in the environmental variable EDITOR, which you naturally have set to vim, right? It also uses the PAGER variable to preview files. To see what that is set to, type echo $PAGER. It’s probably less.

Then, if the mime type does not include text, it looks at the extension of the file. If it’s one of the common ones listed there (xml, json, csv, etc.), it will again use the configured editor or pager program.

Moving down to images:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#-------------------------------------------
# Image Viewing:
#-------------------------------------------
mime ^image/svg, has inkscape, X, flag f = inkscape -- "$@"
mime ^image/svg, has display, X, flag f = display -- "$@"

mime ^image, has pqiv, X, flag f = pqiv -- "$@"
mime ^image, has sxiv, X, flag f = sxiv -- "$@"
mime ^image, has feh, X, flag f = feh -- "$@"
mime ^image, has mirage, X, flag f = mirage -- "$@"
mime ^image, has ristretto, X, flag f = ristretto "$@"
mime ^image, has eog, X, flag f = eog -- "$@"
mime ^image, has eom, X, flag f = eom -- "$@"
mime ^image, has gimp, X, flag f = gimp -- "$@"
ext xcf, X, flag f = gimp -- "$@"

This one looks only at the mime type, and then runs through a bunch of programs that might be installed on your system. If that has xxx line returns true, it will execute the command at the end of that line and exit the flow.

On my Ubuntu system, I do have eog (eye of gnome) installed for image viewing, so that worked great. But on my Mac, none of these worked. In that case, when you try to open a file, it will prompt you for a program to open it with. To fix that, I had to add a new line to the rifle.conf:

1
mime ^image, has qlmanage,  X, flag f = qlmanage -p "$@"

qlmanage is the built-in Mac program otherwise known as “Quick Look”. A simple Google search gave me the command line needed to launch it. Now, no matter which system I’m on, the same config will give me the same behavior, which is a quick view of an image that can be closed by hitting escape. If I were more into editing images, I could swap them out to have them open in PhotoShop or Gimp, or whatever.

Have some new file type that is handled by a specific application? You can pretty easily see how to set up an association for that based on the file extension.

In summary, ranger is a super powerful program. It will take you a bit of time to learn it, but if you’re into working on the command line and prefer keyboard over mouse, you just might love it.