How to set file association in Ranger

File association is used to determine what program to use to open a file. All the file associations for Ranger can be found in ~/.config/ranger/rifle.conf file. This file is well documented and should answer most of your queries. Note that the file is read from top to bottom and the rule that matches first is chosen and applied.

In Ranger, you can choose a program to open a file based on its extension. For example, to open a JPEG file with Feh image viewer, add this line at the top of rifle.conf:

ext jpg = feh "$@"

After the specific file associations, I like to have a rule to catch all the rest of the files and open them using xdg-open:

has xdg-open, flag f = xdg-open "$1"

For more info on configuring Ranger, refer to the excellent documentation by Arch here.

Tried with: Ranger 1.6.1 and Ubuntu 14.04

Advertisements

How to change directory in shell using Ranger

One of the uses of Ranger can be to conveniently change to a directory in the shell that launched Ranger. To do this, we use the --choosedir option of Ranger. Pass this the name of a file and when Ranger is closed it will write the directory it was in to that file. You can then read the contents of this file in your shell to change to that location.

For Bash, a function that can do this is provided in bash_automatic_cd.sh in the Ranger source code.

For Fish, I have written a similar function that you can add to your ~/.config/fish/config.fish:

To use Ranger like this, invoke it at the shell as ranger-cd. When you quit Ranger, fish will change its current directory to that in Ranger.

Tried with: Ranger 1.6.1, Fish 2.0.0 and Ubuntu 14.04

How to create new file using Ranger

You can map any key you want to the operation of creating a new file in Ranger. This can be done using the touch command in the Ranger console.

For example, to create a new file when Ins is pressed, add this to ~/.config/ranger/rc.conf:

$ map <INSERT> console touch

When Ins is pressed, the touch is executed at the Ranger console at the bottom and it asks for a filename for the new file.

Tried with: Ranger 1.6.1 and Ubuntu 14.04

How to use Trash-CLI with Ranger

When a file is deleted using Ranger, that deletion is permanent. Instead, if you would like to delete files to Trash, then you can do that using the Trash-CLI set of tools.

Installing them is easy:

$ sudo apt install trash-cli

The tool we are interested in is trash-put which deletes a file to the Trash. The Trash directory is usually ~/.local/share/Trash. Any key can be mapped in Ranger to this command for deletion.

For example, to map the Del key to delete to Trash, add this to your ~/.config/ranger/rc.conf:

map <DELETE> shell -s trash-put %s

Tried with: Ranger 1.6.1, Trash-CLI 0.12.7-1 and Ubuntu 14.04

How to use Ranger as file explorer for Vim

Vim has an internal file explorer called NetRW. Another popular choice of file explorer for Vim is the NERDTree plugin. Recently, I started to use Ranger as my file explorer at the shell. I could open text files in it using Vim easily. However, what I really wished was to use Ranger from inside Vim as a file explorer.

Using Ranger inside Vim allows me to keep a Vim session with many files open and fall down to Ranger to explore and manipulate files and jump back into my Vim session. Another big advantage is that the files I open using Ranger all persist as buffers in the Vim session. That is, if I am working on File1, I jump to Ranger to find File2 and open it, then it should open back in the same Vim session, so that I now have File1 and File2 open. Thankfully, all of this is possible easily! 🙂

To use Ranger as a file explorer for Vim, all you need to is add this code to your Vimrc. You can then launch Ranger whenever you want by pressing your leader key and r. That is, usually it is \r.

Tried with: Ranger 1.6.1, Vim 7.4 and Ubuntu 14.04

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. Symbols are displayed in 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.

The version control feature is only available in Ranger v1.6.1 and later. If the version from your distribution is older than this, then you need to get the latest version.

Install latest version

  • Before installing the latest version of Ranger, I remove the old one:
$ sudo apt remove ranger
  • Install the latest version of Ranger as described here.

Configure version control feature

  • To enable version control support, have this line in your ~/.config/ranger/rc.conf:
set vcs_aware true
  • Next we need to specify which version control systems we want to be supported. For each version control system, 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, I add this to rc.conf:

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 these lines to rc.conf:
set vcs_aware false
map zg set vcs_aware true

Tried with: Ranger 1.7.2 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 install and use Ranger

Image preview in Ranger
Image preview in Ranger

Ranger is a file manager for the shell with vi keybindings. Its interface is similar to the Finder in Mac OS X. You can move up and down files or directories in the current path by pressing j and k. To move to parent directory use h and to open the directory or file currently highlighted press l. These navigation keys should be very intuitive for the vim user.

Ranger can also utilize many other tools to show previews of many types of files. This is really useful because you can see previews of source code, contents of zip files, text inside PDF files and even ASCII rendering of image files (JPG/PNG) all from inside ranger in color!

Install

If you like the previews, first install those relevant tools, followed by ranger from the Ubuntu repositories:

$ sudo apt install highlight atool w3m poppler-utils caca-utils
$ sudo apt install ranger

The version of Ranger in Ubuntu can be pretty old. To install the latest Ranger from source:

  • Download the latest Ranger source code from Github here:
$ git clone https://github.com/hut/ranger.git
  • Build and install:
$ cd ranger
$ sudo make install

Usage

Run ranger once so that it creates its default configuration directory in ~/.config/ranger. After that run ranger so that it copies over its configuration files to this location. This is necessary to configure ranger or for it to enable previews.

$ ranger
$ ranger --copy-config=all

After this you are good to go: enjoy navigating around your directory and seeing colorful previews of all your files, right in your shell! 🙂

Keyboard shortcuts

Space: Select current file or directory
Del: Delete selected files and directories

S: Open shell in current directory
Ctrl+n: Open new tab
Tab: Switch to next tab
Ctrl+w: Close tab

gh: Go to home directory
gr: Go to root directory
gm: Go to media directory

cw: Rename current file or directory
zh: Show hidden files and directories
dc: Compute size of directory under cursor. Size is displayed in status line at bottom.

On files:
Enter: Open in default program.
i: Open in pager.
r: Open with
o: Change sort order
Space: Select/unselect current file

Tried with: Ranger 1.5.2 and Ubuntu 12.04