Manage Your Files From The Command Line With Ranger

Objective

Install and configure the Ranger file manager.

Distributions

Ranger is available in the repositories of most major distributions.

Requirements

A working Linux install with root privileges.

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

If you like working in the command line, but you want some more guidance and tools than you get with the simple file management commands, it might be time to move to a command line-based file manager, like Ranger.

Ranger enables some powerful features for managing your files while preserving the light weight control that the command line provides. If that’s not enough, Ranger’s controls and commands are all based around Vim bindings.

Install Ranger

Ranger is available in just about every distribution’s main repository. Use your package manager to install it. The package name is almost always ranger.

Navigating With Ranger

Ranger’s layout is based on columns. Moving left through the columns moves you up through the directory tree, ending at the filesystem. Moving right, moves you down through the directories.

In each column(directory) you see a listing of the files and directories inside it. When you finally arrive at a file, Ranger will open it based on how it’s configured to respond to that extension in ~/.config/ranger/rifle.conf.

How do you control Ranger? Use Vim bindings. h moves left. j moves down. k moves up. l moves right. It’s that simple.



Basic Commands

Ranger has built in commands for handling file operations. Once again, these commands are largely based on Vim, so if you’re familiar with Vim, you’ll have no problem adapting.

To copy a file, press yy. Then, to paste it, press ppdd.

You can select multiple files with the space bar before pressing yy or dd to copy or move multiple files at once.

If you want to move multiple files across different directories, you can use ya and da to add each grouping to a list of files being relocated. After you have everything you want to move, use pp to paste/move them all.

Thankfully, exiting Ranger is easier than exiting Vim. Press q to quit.

Command Interface

There’s also a couple of useful commands that require you type into the command interface. Like Vim, Ranger has a command interface that activates when you press :.

Once command that you definitely want is a search. Ranger’s is straightforward.

:search filename

You also probably want to rename files. Again, Ranger makes that simple. Highlight a file and use the command.

:rename newname.txt

Do you want to rename a bunch of files at once? Ranger has an excellent tool for that too. Select the files that you want to rename with the space bar. When you’re done, enter the command to open up your text editor with the list of file names. Edit them there and save. Ranger will do the rest.

:bulkrename


File Associations

When you open up a file with Ranger, it will will search its configuration for the right application and open the file. You can edit that configuration to tell Ranger which programs you prefer to associate with each file type.

Before you go about modifying configurations, you need to copy them. Ranger has an easy command for that too.

$ ranger --copy-config=all

Ranger will create local copies of its configuration files at ~/.config/ranger. It’ll make several files, but for now, you only need one, rifle.conf.

Open up that file in your text editor. The default settings are pretty good. They have a logic behind them. Each grouping is divided up by file type. Those groups contain a series of lines that tell Ranger which programs to look for. They’re listed from least to most common programs in an attempt to cover as many use cases as possible.

The lines all follow this structure.

ext x?html?, has firefox,        X, flag f = firefox -- "$@"

The line begins with ext, which stands for extension. The next part is a regular expression. In this case it is looking for html or xhtml. The next part is the has statement. It’s a listing of programs to check for. In this case, it’s looking if the computer has Firefox and the X server. If it does, it will choose open the file with Firefox. If not, it’ll move on to the next one.

So, for each of these blocks, move the line that fits the programs that you want Ranger to associate with each file type to the top. If there’s something custom that you want, you can add those in too. If you’re familiar with regular expressions, use them. If not, you can always be explicit.

The Editor

The one thing that’s not covered in this file is the $EDITOR environment variable. That determines which text editor Ranger will use for everything, including file associations.

The best way to handle this is to declare the variable in your .bash_rc or .zshrc.

EDITOR=vim

Be sure to restart the terminal or reload the environment before trying it out.

Closing Thoughts

Ranger is an awesome file manager that strikes the perfect balance between graphical file managers and the command line itself. It has powerful built-in tools that make working with files convenient without getting in your way.



Comments and Discussions
Linux Forum