banner

For a list of BASHing data 2 blog posts see the index page.    RSS


Five useful tweaks for the ranger file manager

This week's post is for users of the ranger CLI file manager. It explains how to

I did the tweaks in ranger version 1.9.3 running in sakura terminal emulator.


Preliminaries. The command

ranger --copy-config=rifle && ranger --copy-config=scope && touch ~/.config/ranger/rc.conf

will copy the configuration files rifle.conf and scope.sh to ~/.config/ranger and create a new, blank file rc.conf in that directory. We'll be editing all 3 files.

Note that the ranger user guide says

Be aware that for rc.conf and commands.py, ranger reads both the global and the user's config (in that order)... The best practice is to only add the options/keybindings you actually want to change to your rc.conf, rather than to have a complete copy of the default rc.conf.


Open a file from ranger in a different workspace. This tweak makes use of the wmctrl utility and assumes you're running a compatible window manager. In my Xfce desktop environment (with the xfwm window manager) I have 6 open workspaces. As wmctrl sees them, they're numbered from 0 to 5. The 6th workspace is where I open files that I select with ranger.

ranger uses the configuration file rifle.conf to determine how to open selected files. If I want ranger to open a selected PDF in Firefox in that 6th workspace, I add the line

ext pdf, has firefox, X, flag f = wmctrl -s 5 && firefox "$@"

in the "Documents" section in rifle.conf. The line I've added could be read as "If the file extension is .pdf and there's Firefox and a graphical environment, then run wmctrl to change the working desktop to the 6th one, then open the PDF there in Firefox". Note that if Firefox is already running in another workspace, then the PDF will open in a new tab in the existing instance of Firefox, not in a new Firefox instance in workspace 6.

I routinely open plain text files (.txt, .csv, .tsv, no extension) in Geany text editor, so in rifle.conf I edited

mime ^text, label editor = ${VISUAL:-$EDITOR} -- "$@"

to

mime ^text, label editor = wmctrl -s 5 && geany "$@"

Similar tweaks can be used for other file types. I open .odt and .docx files with LibreOffice Writer:

ext odt|docx, has libreoffice, X, flag f = wmctrl -s 5 && lowriter "$@"

and .xlsx and .gnumeric files with Gnumeric:

ext sxc|xlsx?|xlt|xlw|gnm|gnumeric, has gnumeric, X, flag f = wmctrl -s 5 && gnumeric "$@"


Preview a picture image. Install w3m-img and add this line to your (blank) rc.conf:

set preview_images true

Color image previews will now appear in the right-most column in ranger:

img preview

Preview a PDF as an image, rather than as text. Install poppler-utils and add this line to your (blank) rc.conf if it's not already there:

set preview_images true

In scope.sh, uncomment these lines:

## PDF
  # 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;;

The first page of the PDF now appears in the right-most column, re-sized to fit the column width. (pdftoppm's - singlefile option restricts the image output to the starting page, -f 1.)

pdf image

Preview an Excel file differently. By default, ranger uses xlsx2csv in scope.sh to preview Excel files. In other words, it previews a CSV as text. The result can be a bit hard to understand, especially if there are lots of fields in the spreadsheet, because a wide CSV will get truncated:

Excel1

View of complete preview pane (right) and part of middle pane (left).

I changed the instructions for previewing Excel files in scope.sh to show a complete list of fields and a corresponding list of entries from just the first data row:

## XLSX
  xlsx)
   ## Preview as csv conversion
   ## Uses: https://github.com/dilshod/xlsx2csv
   ## xlsx2csv -- "${FILE_PATH}" && exit 5    #the ranger default
   in2csv "${FILE_PATH}" | sed -n '1,2p' | datamash -t"," transpose | column -s"," -t && exit 5    #my tweak
   exit 1;;

Excel2

I use in2csv from csvkit to generate a CSV from the Excel file. The CSV is piped to sed to print just the first two lines, i.e. the header and the first data row. The two rows are piped to GNU datamash to transpose rows and columns, and the transposed table is piped to column to arrange the columns neatly.
 
This doesn't always work. One reason for failure is linebreaks embedded in Excel cells, which neither in2csv nor xlsx2csv will modify. The resulting CSV has lines with different numbers of fields, and datamash won't transpose a file with different field numbers. The same failure occurs if the spreadsheet has caption or comment lines. A workaround for the datamash issue is to add the --no-strict option, but the result isn't so tidy and header and first-line items may not correspond properly. I haven't seen failures yet with simply structured Excel tables.


Preview plain-text email messages. Following this suggestion, edit scope.sh as follows:

## Text
text/* | */xml)
 
to
 
## Text
text/* | */xml | message/rfc822)


I'm otherwise comfortable with the ranger defaults. One minor tweak was to install odt2txt for ODT previews. I launch ranger in sakura terminal on my desktop with a keyboard shortcut for the command

sakura -c 160 -r 55 -d /home/bob/Desktop -e /usr/bin/ranger

This opens ranger with my home directory as the top one (in the left-most ranger column). I could add the sakura option -n 2 to generate a new tab in the terminal window for command-line work, but I prefer to do that work in a separate instance of sakura in another workspace.


Last update: 2024-06-14
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License