Ranger is a free and open source file manager written in Python. It is designed to work from the command line and its keybindings are inspired by the Vim text editor. The application has a lot of features and, working together with other utilities, can display previews for a vast range of files. In this tutorial we learn how to use it, and explore some of its functionality.
In this tutorial you will learn:
- How to install Ranger on the most used Linux distributions
- How to launch Ranger and copy its configuration files locally
- Ranger basic movements and keybindings
- How to visualize hidden files
- How to get preview of various types of documents
- How to create, access and remove bookmarks
- How to select files and perform actions on them
Software requirements and conventions used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Distribution independent |
Software | Ranger, pdftoppm and w3m-img for pdf and image previews, transmission to visualize torrent information |
Other | none |
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 |
Installation
The Ranger file explorer is written in Python and is available in the official repositories of all the most used Linux distributions, therefore we can install by simply using their respective package managers. If using Debian or one of its derivatives, one of the choices is to use apt-get
:
$ sudo apt-get update && sudo apt-get install ranger
On Fedora we use the dnf
package manager instead. Installing the “ranger” package is just a matter of execute the following:
$ sudo dnf install ranger
Archlinux is another famous distribution, so it needs no presentations. We can install the “ranger” package from the “Community” repository by using the pacman
package manager:
$ sudo pacman -Sy ranger
Launching the file manager
To launch Ranger, all we have to do is to invoke it from a terminal emulator or tty. Once launched, the application should look like this:
The file manager uses our current working directory as its starting point ($HOME in this case). We can, however, specify the starting directory explicitly, by passing it as an argument when invoking the file manager; to open ranger and use the ~/Downloads
directory as starting point, we would run:
$ ranger ~/Downloads
One important thing we must do the first time we use ranger, is to copy the default configuration files in the local ~/.config/ranger
directory; doing so we will be able to personalize ranger without the need of administrative privileges. We can perform the action by running:
$ ranger --copy-config=all
The command will return the following output, which confirms the files have been copied:
creating: /home/egdoc/.config/ranger/rifle.conf creating: /home/egdoc/.config/ranger/commands.py creating: /home/egdoc/.config/ranger/commands_full.py creating: /home/egdoc/.config/ranger/rc.conf creating: /home/egdoc/.config/ranger/scope.sh
What is the scope of those files? Let’s see it briefly:
File | Function |
---|---|
rifle.conf | Configuration for “rifle”, the ranger file launcher |
commands.py | A Python module which defines the ranger console commands |
commands_full.py | Ignored: it is only used as a reference for custom commands |
rc.conf | Contains ranger keybindings and settings |
scope.sh | Defines how to handle file previews |
Visualize hidden files
By default hidden files and directories are not displayed (they are those which the name starts with a dot). To visualize them we should issue a command. Just like we do in Vim, we press the :
key, and write the following:
set show hidden true
With the above command we set the show hidden
option to true
. The change will be effective as soon as we press Enter
, however it will not survive when the application will be closed. To make this and others settings persistent, we should write them in the “main” configuration file, which, as we saw above, is ~/.config/ranger/rc.conf
.
Basic movements and keybindings
Ranger keybindings are inspired by the Vim text editor; to move up and down in the list of directories and files, for example, we can use the k
and j
keys, respectively. Once a directory is selected, its content is displayed in Ranger rightmost column. The current directory parent, instead, is displayed in the leftmost one:
To enter a directory, once it is selected, we can press the Enter
key, press l
, or use the right arrow key. To move to its parent directory, instead, we can press the h
key (h
and l
are the key used in vim to move left and right in the body of a document, when in normal mode). Just like we do in Vim, we can specify the number of movements to perform, before a key: to move two times down, for example, we can press 2j
. By Pressing G
we will move to the bottom of a
list; with gg
we will move to the top, instead.
Copying, moving and deleting files
To copy a file when using Ranger, all we have to do is to select it and press yy
, to paste it, instead, we can press p
. To move files, what we do is pressing dd
to “cut” them, and then p
to paste them in the appropriate place. Finally, do delete a file, we press dD
. All of these actions can also be accomplished by running the appropriate commands: copy
, paste
, rename <newname>
and delete
, respectively.
These are just the basics: take a look at the Ranger manual to learn all Ranger keybindings.
Getting the preview of a document
As we saw, when we select a directory we can visualize its content in the leftmost column of the application. In the same fashion we can get a preview of an existing text file when it is selected. In the picture below, we can see the content of the ~/.bash_logout
file:
On specific terminal emulators like “rxvt-unicode” or “xterm”, and with the help of dedicated external programs and utilities, Ranger can show previews of other type of files. Let’s see some examples.
Previewing pdf and images
By default, pdf files are “previewed” as text; with the pdftoppm
(part of the poppler-utils
package) and the w3m-img
package installed (it is called w3m
in Archlinux), however, Ranger can preview them as images. For this feature to work the preview_images
option must be set to true
and we should perform some changes in the scope.sh
file. This file is a simple shell script used to determine how to handle various file extensions. What we want to
do, is to uncomment lines 163
to 170
:
application/pdf)
pdftoppm -f 1 -l 1 \
-scale-to-x "${DEFAULT_SIZE%x*}" \
-scale-to-y -1 \
-singlefile \
-jpeg -tiffcompression jpeg \
-- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \
&& exit 6 || exit 1;;
If all is set correctly the first page of a pdf should appear as its preview when we select it:
With the w3m-img
package installed, Ranger will also show image previews:
Showing torrent information
Ranger is also able to display information about torrent files: it relies on the transmission
application to do so, therefore it must be installed on our system for this feature to work. Below is an example of the info displayed for
the ubuntu-21.04-desktop-amd64.iso.torrent
file:
Creating, accessing and removing bookmarks
The ability to create bookmarks is essential in all file manager applications.To create a bookmark in Ranger, all we have to do is to press the m
key followed by the letter or digit we want to associate with a directory, once in it. Let’s
see an example. Suppose we want to associate the m
key to the ~/Music
directory. Once we are inside it, we press m
; a list of the current bookmarks will be displayed:
At this point we press m
again (this is the letter we want to associate with our directory) to create the bookmark.
Once the bookmark is created, in order to access it, we have to press the `
key followed by the letter (or digit) associated with the bookmark, so in this case `m
.
To remove an existing bookmark, all we have to do is to press the um
keys followed by the key associated with the bookmark we want to remove. To remove the bookmark we set above, for example, we would press umm
.
Bookmarks can be saved instantly or when we exit Ranger. The option which controls this behavior is autosave_bookmarks
, and accepts a boolean value. It is usually set to true
by default.
Selecting files
To select one or multiple files when working in Ranger, all we have to do is to “mark” them by pressing the <Space>
key. Once we do that, a yellow Mrk
symbol will appear at the bottom right, and the selected files will be highlighted:
Once the files are selected, we can apply an action to all of them at once. For example, in order do delete them, we would enter the delete
command or press the dD
keys. When deleting files, a prompt will appear and ask us to
confirm the action.
When one or more file are selected, we can also run a shell command directly on them. To do so all we have to do is to press the @
key; this will make the :shell %s
prompt appear at the bottom of the interface, with the cursor positioned before %s
, which basically means “all selected files in the current directory”. The command we type will be applied to all selected files.
Conclusions
In this tutorial we learned how to install Ranger, and its basic usage. We learned how to copy its configuration files locally, and what is their purpose, how to perform the basic movements and actions with Vim-inspired keybindings, some examples of getting the preview of pdf,images and torrent files, how to set option values at runtime and permanently, and how to create, remove and access bookmarks. Finally, we saw how to select files and run commands on them. We just scratched the surface of Ranger usage: take a look at the application manual to learn
everything you can do with it!