How to set editor used by Ranger

You might find that Ranger uses Nano as editor when you open text files. It does this if the EDITOR environment variable is not set.

To make Ranger use a different editor to open text files, just set this environment variable.

For example, in the Fish shell, I set the editor to vim by adding a line to ~/.config/fish/config.fish:

set -x EDITOR vim

Tried with: Ranger 1.6.1 and Ubuntu 14.04

How to enable version control in Ranger

Ranger can show the version control status of files and directories. Various symbols are displayed in appropriate colors along the right side, beside the file size, to indicate the version control status of those files. For example, a green tick mark is shown if the file is up-to-date. Version control systems that are supported include Git, Mercurial (Hg) and Bazaar.

To enable this feature, you need version 1.6.1 or later. If the version from your distribution is older than this, then you can download the Ranger source code from here. Installing from source is easy:

$ sudo make install

Before proceeding further, request Ranger to generate the necessary config files by using this command:

$ ranger --copy-config=all

To enable version control support, have this line in your ~/.config/ranger/rc.conf:

set vcs_aware true

Next we need to enable the version control systems which we want supported. There are three possible options: disabled, local (use only local information) and enabled (get remote information too).

For example, to enable Git and Mercurial support with only local information:

set vcs_backend_git local
set vcs_backend_hg local

Open Ranger now in a directory that is inside any repository and you will see the file status information displayed at the right side of the column.

I found that having VCS enabled made Ranger extremely slow to start. So, I disabled it by default and instead mapped a keyboard shortcut zg to enable it when I needed it. This can be done by adding:

set vcs_aware false
map zg set vcs_aware true

Tried with: Ranger 1.6.1 and Ubuntu 14.04

Color Du

du is one of those commands with a good indent, but not useful in practice. This is because it shows the size of directories, but in numbers which make it hard to quickly see which files or directories are the biggest. After all, that is the reason why this tool is mostly used.

Color DU (cdu) is a program written by Stephane Levant that fixes this problem beautifully. Its display is similar to du, one file or directory per line, but it shows a colorful bar graph of usage. It becomes immediately apparent which directories or files are hogging all the space in a given directory.

To install this program:

  • Download its source code here and unzip the contents.

  • Build and install it:

$ make
$ sudo make install

Tried with: CDU 0.37 and Ubuntu 14.04

How to preview images in Ranger

Ranger is a great console tool to explore directories and files. You can find the instructions to install and configure it in this post. As you explore files in the center column, Ranger displays a preview of the file contents in the right column. If the file is an image, then Ranger displays a color ASCII version of the image using the Caca library.

However, if you use XTerm or a terminal with similar capabilities, Ranger can show an actual image preview with full color and pixels! Remember that this works only on XTerm or similar terminals.

To preview images like this, Ranger requires the w3mimgdisplay program. You can get it by installing:

$ sudo apt install w3m-img

After this, you need to set the preview_images option to true in the .config/ranger/rc.conf file. You will have this file only if you followed the instructions given above during Ranger installation.

Once you have done this, open an XTerm and enjoy full-color image previews! Note that this did not work in RXVT for me :-)

Tried with: W3m-Img 0.5.3-15, Ranger 1.6.0 and Ubuntu 14.04

How to output with timestamp using ts

You might sometimes want to create a log of the output from a program. Going into the program and adding a timestamp for each print command might seem cumbersome or not possible. It is here that the tiny ts utility makes itself useful. Pipe any output through it and it prefixes a timestamp to each line. Pipe the output of a long-running program through it and then save it to a file and you have a log!

To get this program, install the moreutils package:

$ sudo apt install moreutils

To use it:

$ ./my-program | ts > log.txt

Tried with: MoreUtils 0.5 and Ubuntu 14.04