Ranger: Lightning Fast File Management

Ranger: Lightning Fast File Management

Article from Issue 203/2017
Author(s):

Stop fiddling around with the mouse or trackpad – do your file management in the terminal, with vi-like key bindings.

Back in the late 1990s and early 2000s, as Linux was starting to get some serious attention, it was very difficult for Linux advocates to extol the benefits of the command line. Most DOS or Windows users had never seen a powerful command-line shell before – all they knew was the bare-bones, featureless, clumsy DOS prompt. They thought that was everything that was possible. Why switch to this weird Linux thing when you apparently have to do all the work in a DOS-prompt-type box?

Of course, you knew that Bash (like many other Unix shells) was a million miles ahead of the DOS prompt, but it was a hard sell. Over the years, however, the situation has improved considerably. Many Windows and Mac users have never been exposed to a command line before, so they have no negative preconceptions of how they work. We FOSS fans can demonstrate some cool time savers at the Bash prompt and impress potential convertees. (In fairness to Microsoft, PowerShell is a huge step up from the DOS prompt as well.)

While it's worth learning Bash shortcuts, key bindings, and scripts to make life easier, there are so many great terminal apps as well. Think of the Vim and Emacs editors, the Mutt email client, or the plethora of awesome development tools. But one thing you may have never tried is a command-line file manager. That may sound odd – after all, you can already manage files at the command line with cp, mv, rm, and other tools, right? Why would you need a separate app to do those jobs?

Well, for certain tasks, those standalone commands work pretty well; but sometimes you want something that's halfway between raw command-line tools and a graphical file manager. One of the best apps in this respect is ranger [1], which describes itself as "a console file manager with Vi key bindings, providing a minimalistic and nice Curses interface with a view on the directory hierarchy".

If you love the vi(m) text editor, this may sound like heaven already, but if you've never used vi before, or you tried it and hated it, stay with me! You know that vi has a steep learning curve and some of the key bindings seem awkward at first, but it's worth learning them, and to use ranger effectively, you don't need to learn a whole lot of them in any case. So let's get started.

First Steps

Ranger is available in the package repositories of almost every major Linux distro, so just fire up your package manager and grab it. Alternatively, you can always get the very latest stable release – or a testing version from the Git repository – on the downloads page [2]. Once you have it installed, you can start it from the command line simply by entering:

ranger

You'll see something like Figure 1, ranger's default layout, but what exactly is going on here? Well, the column on the left shows the current directory; in this case, it's my home directory /home/mike. The second column can be used for browsing, showing a list of files and directories in the current directory. You can see that I've selected a README file, and the contents of it are being displayed in the right-hand panel – like an instant preview. How was this file selected, though?

Figure 1: Here's a freshly started ranger, showing a preview of a text file in the right-hand pane.

Try using the Up and Down arrow keys to select items in the middle panel. Then try the more vi-like approach: Press the j key (lowercase) to move down, and the k key to move up. (If you've never used vi before, you might be scratching your head about this seemingly arbitrary choice of keys – why j and k? The answer is: They're on the "home row" of the keyboard, roughly in the middle, where your fingers are when you're not typing. The idea is that they're easy to reach at any point, as opposed to arrow keys, which are typically in the corner.)

Along with this three-pane layout are extra information lines at the top and bottom of the window. The top line shows your username and the hostname of the your machine (like a typical Bash prompt), along with the file or directory currently being displayed. In the bottom-left, meanwhile, you'll find more details about the file or directory: permissions, owner, size, and timestamp (when the file was last modified). Finally, the text in the bottom-right shows the total size of files in the current directory, along with free disk space.

So far we've only looked at the current directory, but to change directories, you can use the arrow keys: Press Right to go into the currently selected directory, or Left to go into the parent directory. However, you can also use vi-style keys: h (again lowercase) in place of the Left arrow, and l in place of the Right. This means you can do all navigation with h, j, k, and l – so four keys in the same area of the home row. It may seem a bit odd at first, and certainly requires some practice, but once you get used to it, you'll stop reaching down to the arrow keys.

Note that you can press Right arrow (or l) to go into a directory, but try doing the same operation on a plain text file. You'll be prompted to select an editor, and then you can edit the file in place. Quit the editor and you'll return to ranger, exactly where you were before. If you just want to view the contents of a file without editing it, press i (lowercase), and then q or Esc to return to ranger.

To quit ranger itself, press the colon (:) key to bring up a prompt at the bottom of the screen, and then tap q and press Enter. (Or, as a Vim-like shortcut, just press Z and then Q.)

Navigation and Tagging

I've looked at the main keys for navigating around, but plenty of other vi-like shortcuts come in handy, as well. In long lists of files or directories, Ctrl+F and Ctrl+B scroll through pages, forward and backward, respectively. You can also use J and K (capital letters in this case) to scroll through half pages. To jump immediately to the bottom of a list, hit G.

Now try pressing g (lowercase) on its own – you'll see that a panel listing various shortcuts appears (see Figure 2). These are keys you can press after tapping g to jump to other places in the filesystem. For instance, hitting g followed by r immediately takes you to the root (/) directory – a very handy shortcut when you're doing administration work. (Ranger is great to use over SSH connections when you're doing some admin jobs on a remote machine.)

Figure 2: Ranger is packed with time-saving keyboard shortcuts – here's the list shown when you press g.

Say you're in /home/user/Downloads/foobar, and you hit g-r (g and then r) to jump to the root directory, but then want to go back to where you were previously. It's a bit laborious to navigate back into /home, and then user, and Downloads, etc; however, you can press H (capital this time) to go back through your navigation history. Just one tap of H, and you're back to where you were. Try pressing g again and exploring some of the other options, such as h to jump to your home directory or e to pop quickly into /etc.

In the list of shortcuts following g, have a look at the last four: these let you create and manage tabs in ranger, much like the tabs in a web browser. For instance, hit g-n to create a new tab – that is, a new view in the file manager. In the top-right corner, you'll now see the numbers 1 and 2, with the latter being highlighted. This is the currently active tab. Hit g followed by T or to move right and left in the list of tabs, respectively. To close a tab, press g followed by c. Tabs are especially useful if you're doing a job in one location in the filesystem and quickly want to do some work elsewhere, without having to keep jumping around your navigation history.

What about situations where you want to add a label or mark to a file or directory, so that it stands out? Many graphical file managers let you assign colors or icons to files, and ranger has something similar in the form of tagging. Select a file or directory, and then press the t key – you'll see that a red asterisk (*) appears next to it. The file or directory hasn't been modified, however, and these tags will remain even if you close and reopen ranger.

You can even create custom tags as alternatives to the asterisk: press the double quote (") key followed by the letter, number, or symbol you want to use for tagging. To remove any tag from a file or directory, just highlight it and press t.

Managing Files

Up to this point, I've focused on navigation, and you've seen that many key combos and shortcuts can help you zip around your filesystem, but the other job of a file manager is, of course, managing files! Ranger includes various vi-style key operations for performing these tasks.

Start with copying: Highlight the file (or directory) that you want to copy, and press y-y (lowercase Y, twice in a row). In vi terms, this "yanks" the item, making a copy of it on the clipboard, which you can then paste into another directory. Navigate somewhere else, and press p-p (two lowercase Ps) to place the copy there. (If you copy and paste a file or directory in the same location, they both can't have the same names, so the "pasted" version will have an underscore at the end.)

To move a file into a different directory, you can do the same as above, replacing the y-y keystrokes with d-d – think of it like cut and paste, rather than copy and paste, although note that the file doesn't actually get moved until the p-p (paste) operation. If you just want to rename a file or directory, tap c to bring up a list of options and then w; this gives you a prompt for the new name at the bottom of the screen (which also handily supports tab completion).

When you want to delete a file, tap d to bring up a menu, and then D (capital this time), which puts a prompt at the bottom of the screen – hit Enter here to confirm the delete operation, or Esc if you want to cancel it. Note that it's possible to do copy, move, and delete operations on multiple files: Highlight the ones you want, pressing the space bar as you go over them, and you'll see that they change to a different color (yellow in the default setup). Once you have a number of files or directories marked in yellow, you can use the previously mentioned commands on them as a group.

To search for files, hit / (forward slash), which brings up a prompt at the bottom. Type the letters you want to search for and hit Enter, and ranger will highlight the first matching file. To move to the next match, tap n (or press N to go backwards through the search results).

Various options are available to change the sort order of the file listings – tap o to see these (Figure 3). The two most useful options are b (to sort by "basename," i.e., the filename) and s (to sort by size). Various other display options can be activated by pressing z, which brings up a list of keystrokes for showing hidden files and disabling the preview panel, in case you don't want it, or your terminal dimensions are rather limited. Indeed, if you'd rather open files in separate apps rather than having ranger try to display them, you can highlight the file(s), press the r key, and then type the name of the app you want to use.

Figure 3: Want to change the listing order? Tap o to see what's possible.

I've covered a lot of keypresses and combos over the last few pages, but as you spend time using them, they become second nature, and you'll find yourself working with files at a blistering pace. I've looked at ranger's main feature set here, but you might want to explore some other goodies, such as bookmarks, macros, and plugins. To learn more about these, press ? inside ranger and then m to view the manual page. (See also the "Image Previews" box.)

Image Previews

If you're working with lots of images in ranger, you may get tired of hitting Enter on each one to see what it looks like. Wouldn't it be better if you could view the images directly in the preview pane, inside the file manager itself? Well, this is possible – and it's more advanced than blocky ASCII art.

First of all, you'll need to install the w3m command-line web browser, and its w3m-img tool. In Debian- and Ubuntu-based distros, you can grab them with:

sudo apt-get install w3m w3m-img

They might have different names in other distros, but just search for "w3m" and you should be able to find them. (The w3m-img tool may also be included in the main package.)

Next, in ranger, hit the colon (:) key to bring up a prompt at the bottom, and then enter set preview_images true. Now, when you highlight an image file, it should be displayed in its full pixelated glory in the right-hand preview panel, as in Figure 4.

Figure 4: With the help of w3m, you can even preview images inside the console!

This is great, but if you really want to go old school and use ASCII art for your image previews, you can do that as well – look at the ranger wiki and scroll down to the "ASCII previews" bit [3].

You can also tap ? followed by c to view a list of advanced commands that are available at the prompt, brought up by pressing the colon (:) key. To really get the most out of ranger, I recommend printing out the key combo cheat sheet [4] (Figure 5) and sticking it on your wall!

Figure 5: Ranger's website has a lovely keyboard shortcut cheat sheet that's worth printing out.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Ranger

    Ranger offers a wealth of commands – many with alternatives – and less reliance on a mouse.

  • Ryzom

    Fancy an adventure? Then travel to the futuristic planet Atys, where a large world with many exciting missions awaits.

  • Mediapurge

    If you have a download folder full of photos and music, Mediapurge can help you sort files and even remove duplicates, but beware of its quirks.

  • CLI File Manager

    If you're a Linux lover, you'll know the command line is the slickest and most efficient way to interact with the system. Free yourself from point-and-click with the nnn command-line file manager.

  • Czkawka

    Czkawka helps find and remove duplicate and obsolete files to free up valuable disk space.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News