A modern file manager for the command line

Command Line – ranger

© Photo by Brian Mann on Unsplash

© Photo by Brian Mann on Unsplash

Article from Issue 251/2021
Author(s):

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

Linux has all the necessary commands for manipulating files, everything from ls and cp to rm and mv. However, file managers have the advantage of centralizing all the commands and requiring less knowledge in all except the most advanced circumstances. In fact, file managers remain so useful that Midnight Commander (mc) continues to be used at the command line 27 years after it was first released. A more recent command-line file manager is ranger [1], which uses many Vim keybindings and is written in Python, which allows for the easy creation of custom scripts.

Ranger is available in many distributions, and the latest version can be downloaded from GitHub [1]. It requires Python 3 and includes a long list of suggested packages for full functionality, such as w3m for graphics display, poppler-utils for PDF display, and python-pygments for code highlighting. It creates the directory ~/.config/ranger/rc.conf with the configuration files in Table 1 (shown in Figure 1).

Table 1

Ranger Configuration Files

rifle.conf

Configuration for rifle, the ranger file launcher

commands.py

Defines the default ranger console commands

commands_full.py

Reference file for custom commands

rc.conf

The main configuration file

scope.sh

Defines how to handle preview files

Figure 1: Ranger distinguishes carefully between global and personal config files, suggesting you bypass the global copy and edit the personal copy.

The configuration files also function as documentation on how to customize; although, unless you have a knowledge of Python, these files may be of limited use. A few notes are given in the GitHub repository [2].

Upon startup, ranger first references the global configuration in /etc/ranger/rc.conf by default and then ~/.config/ranger/rc.conf. To avoid potential conflicts, you should add to the top of rc.conf the line RANGER_LOAD_DEFAULT_RC =FALSE so that ranger bypasses the global configuration, and make all your edits in ~/.config/ranger (Figure 2). While making this change, you might also scan the local rc.conf for other customizations, such as whether ranger displays hidden files or if deletions require confirmation. The other files can be ignored unless you want to customize.

Figure 2: The basic configuration file for ranger.

Basic Navigation

If ranger is started as a bare command, it displays your home directory. However, you can also follow the command with a space-separated list of directories and files. A few options are available, but most are unnecessary, especially if you add a list of directories. For many, the only useful option may be --show-only-dirs. Each directory or file specified is opened in a tab in the order in which it is listed in the command. The number of directories or files open is listed in the upper right corner. To switch to another tab, press Tab or gt to move to the next tab, or Shift+Tab or gT to move to the previous tab. The keybinding gc will close the current tab, and Ctrl+N or gn opens a new tab. In addition, you may want to enter the command:

<C>:set show hidden true<C>

or add it to ~/.config/ranger/rc.conf to show hidden files as the permanent default.

Each tab has three panes (Figure 3). The left-hand pane shows the top level directories, and the middle one the second level subdirectories. The right-hand pane shows the third-level subdirectories and files, with statistics at the bottom. Types of files are color-coded – for instance, PDF files are white, JPEGs orange, and FLAC files pink. Basic navigation is with arrow keys, with more complex commands assigned to other keys and key combinations (see Table 2). Default key assignments are based on Vim or mc, making ranger easy for experienced command line users to learn. An online summary of basic navigation displays when you press the g key.

Table 2

Basic ranger Navigation

Up arrow, Page Up

Up a page

Ctrl+U

Up half page

Down arrow

Go down

Ctrl+F, Page Down

Down a page

Ctrl+D

Go down half page

Left arrow

Go left

Right arrow

Go right

Home

Move to top

End

Move to bottom

cd

Change the working directory to a specific directory

gh

Change the working directory to your home

gm

Change the working directory to /media

gM

Change the working directory to /mnt

gu

Change the working directory to /usr

H

Back in history

L

Forward in history

q

Quit

Figure 3: The ranger main screen.

Copying, Moving, and Deleting

To access basic file management, press y for a list of copy and paste commands and d for move or delete commands (Figure 4). Note that, like Vim, ranger uses the term "yank" instead of paste. Besides the basic commands, the lists also contain advanced options for the basic keystrokes (Table 3). Alternatively, you can press r with a file selected or the s key and use the standard shell commands instead.

Table 3

Basic File Manipulation

yy

Copy

yp

Paste (yank)

yd

Paste directory (yank)

dd

Cut

dD

Delete (yank)

ya, da

Add selection to a list of files

yr, dr

Remove selection from a list of files

yt, dt

Toggle list of files on or off

Figure 4: Ranger includes built-in cheat sheets for easy use.

For actions involving more than one file, you can use copy or cut modes. Pressing a adds a selected file to the multiple files, while r removes the selected file. When you are finished creating a list of files, you can proceed with the operation by pressing t to toggle to the copy or cut mode.

To rename or move a selection, press wa. For renaming multiple files or directories, you can also use the :bulkrename command.

Viewing and Searching

Although ranger is a command-line application, it can open other applications to view formats that include PDF, OpenDocument (LibreOffice), BitTorrent, HTML, and SVG. The supported formats and how they are handled are listed in the scope.sh configuration file. You can use the examples in scope.sh to add support for other formats. If a file is properly configured in scope.sh, all you need to do is select the file and press the Enter key. Some files display in the right-hand pane (Figure 5), and others open in an application in a new window. If a file opens in the righthand pane, but you would like to view it in its native application, you can use the :travel command. Should a file type not be supported, you can open a command line and run the rifle command, which supports a wide variety of formats.

Figure 5: Ranger views text files in the main window and opens other applications for viewing other file formats.

At times, you may just want to view file attributes with :linemode. The command can be completed by filename, permissions, fileinfo, mtime (last time modified), or sizemtime (the size and time of the last modification). You can modify these completions by using them with :meta.

The :travel command can also be used to quickly find directories. Just enter the name of the directory at the end of the command. Another alternative is to create a bookmark for a selected directory by pressing m and assigning a letter or number. A list of existing bookmarks displays as a crib, and you can create the bookmark by pressing m a second time. To jump to a bookmark, press ` followed by the bookmark's letter or number.

Within directories, you can use / or f to find a file. If the file is not found, you can search for it in the next file by pressing n, or in the previous directory by pressing N. Visual searches can be enhanced with :filter REGEX to reduce the number of files displayed. You can also sort by file size with os or arrange sub-directories by size with du.

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

  • Tutorials – Ranger

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

  • Command Line: YankRing

    With the YankRing plugin, Vim's yank and pull features become even more powerful.

  • Fsniper

    Every day, computers are inundated with hundreds of files. Fsniper welcomes the new arrivals and processes them according to rules that you define.

  • Ryzom

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

  • broot

    The broot file manager guarantees clearer, quicker navigation of the directory tree at the command line.

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