Tutorial: Integrating ranger and sxiv with Advanced Features
Table of Contents 📑
This tutorial combines all the necessary steps to seamlessly integrate sxiv and ranger, including:
- Launching a gallery view from ranger
- Configuring keybindings in ranger
- Creating an interactive “Open with…” menu in sxiv
- Opening ranger from within sxiv
- Navigating and using shortcuts in sxiv
- Increasing thumbnail sizes by recompiling sxiv
Prerequisites
- Arch Linux (or a similar distro; this guide refers to
pacmanand the AUR) sxivandrangerinstalled- A terminal emulator (e.g.,
xterm,urxvt,alacritty,kitty) - Standard command-line tools (
file,grep,sed, etc.) gtk-launch(usually available viaxdg-utilsor a similar package)
1. Set Up the ranger Keybinding (Launch Gallery)
Open your ranger configuration file:
~/.config/ranger/rc.conf.Add a line to open sxiv in thumbnail mode recursively using a key combination. For example,
gG:# Open sxiv in thumbnail mode (-t) recursively (-r) in the current directory (%d) map gG shell sxiv -tr %d &-tenables thumbnail mode.-rsearches subdirectories recursively.- The trailing
&ensures that ranger does not wait for sxiv to close.
Save the file and reload the configuration in ranger with:
:reload_configor simply restart ranger.
2. Navigating in sxiv
Open Gallery View: Use
sxiv -tr /path/to/your/folderto start directly in thumbnail mode.Open a Single Image: Highlight an image in the gallery and press
Enter.Switch Between Images (Single-Image Mode):
norSpace: Next imageporBackspace: Previous image]: 10 images forward[: 10 images backwardg: First image,G: Last image- Right mouse click: Next image, Left mouse click: Previous image
Zoom in the Gallery:
+: Zoom in-: Zoom out
To increase the maximum zoom level, you need to recompile sxiv (see Section 4).
3. Interactive “Open with…” and Ranger Integration in sxiv
We will create a key-handler script that allows the following:
Ctrl-x r: Opens ranger in a new terminal, selecting the current file.Ctrl-x o(or any other key): Displays an interactive menu of all suitable applications and opens the image with the selected one.
3.1. Create the Script
Create the directory:
mkdir -p ~/.config/sxiv/execCreate the file
~/.config/sxiv/exec/key-handlerand paste the following content in its entirety:#!/usr/bin/env sh # # ~/.config/sxiv/exec/key-handler # Interactive script for sxiv: # - Ctrl-x r → opens ranger in a new terminal with the current file selected # - Ctrl-x o (and others) → opens an "Open with…" menu to choose an application # Robust handling of spaces, logging, and i3-compatible terminals # Logfile for debugging and tracking LOG=/tmp/sxiv-open.log echo "=== key-handler started $(date) ===" >>"$LOG" # Terminal emulator used to open the selection menu # Possible options: xterm, urxvt, alacritty, kitty … TERMWIN=${TERMWIN:-urxvt} echo "TERMWIN set to: $TERMWIN" >>"$LOG" # $1 contains the key pressed after Ctrl-x cmd="$1" echo "Command argument: $cmd" >>"$LOG" # Remove the prefix argument; all image paths follow via stdin shift # Process each file individually (handles paths with spaces) while IFS= read -r img; do echo "Processing file: '$img'" >>"$LOG" # === Ranger Integration (Ctrl-x r) === if [ "$cmd" = "r" ]; then echo "Ranger Integration: Opening ranger in terminal for '$img'" >>"$LOG" # Ranger needs a terminal, so launch it via a terminal emulator "$TERMWIN" -e ranger --selectfile="$img" & continue fi # === Default Open-with… Menu (Ctrl-x o or other keys) === # 1) Determine the MIME type of the file mime=$(file --mime-type -b "$img") echo " MIME-Type: $mime" >>"$LOG" # 2) Find matching .desktop files for this MIME type mapfile -t apps < <( grep -Rl "MimeType=.*$mime" \ /usr/share/applications ~/.local/share/applications 2>/dev/null \ | xargs -r -n1 basename \ | sort -u ) if [ ${#apps[@]} -eq 0 ]; then echo " No matching applications found for MIME '$mime'" >>"$LOG" continue fi echo " Found applications: ${apps[*]}" >>"$LOG" # 3) Create temporary files for the list and the choice list_file=$(mktemp) tmp_choice=$(mktemp) printf "%s\n" "${apps[@]}" >"$list_file" echo " List file: $list_file" >>"$LOG" echo " Choice file: $tmp_choice" >>"$LOG" # 4) Open the terminal selection menu "$TERMWIN" -e bash -lc ' PS3="Open with: " # Read all entries into an array IFS=$'\''\n'\'' read -rd "" -r -a options < "'"$list_file"'" select opt in "${options[@]}"; do # Write the choice to the temp file echo "$opt" > "'"$tmp_choice"'" break done ' # 5) Read the choice and clean up choice=$(<"$tmp_choice") rm -f "$list_file" "$tmp_choice" echo " Selected: $choice" >>"$LOG" [ -n "$choice" ] || continue # 6) Find the full .desktop file path desktop="" for d in /usr/share/applications ~/.local/share/applications; do if [ -f "$d/$choice" ]; then desktop="$d/$choice" break fi done echo " Desktop entry: $desktop" >>"$LOG" # 7) Read the Exec line and remove placeholders exec_cmd=$(grep -m1 '^Exec=' "$desktop" \ | cut -d= -f2- \ | sed -E 's/ %[fFuUdDnNickvm]//g') echo " Exec command: $exec_cmd" >>"$LOG" # 8) Execute the program with the image path eval "$exec_cmd \"$img\"" & echo " Application started for '$img'" >>"$LOG" done # End of log echo "=== key-handler finished ===" >>"$LOG"Make the script executable:
chmod +x ~/.config/sxiv/exec/key-handlerTERMWINvariable (optional): If you want to use a terminal other thanurxvt, likealacritty, add this to your~/.bashrc/~/.profile:export TERMWIN=alacritty
4. Thumbnail Sizes (Optional: Recompiling sxiv)
By default, the available thumbnail sizes in sxiv are limited to { 32, 64, 96, 128, 160 } px. If 160px is too small, you can increase them as follows:
Get the sources from the AUR (example using
sxiv-git):git clone https://aur.archlinux.org/sxiv-git.git cd sxiv-gitGenerate
config.h:make config.hAdjust Thumbnail Sizes: Open the
config.hfile and find the_THUMBS_CONFIGsection. Change the block to something like this:#ifdef _THUMBS_CONFIG /* thumbnail sizes in pixels (width == height): */ static const int thumb_sizes[] = { 32, 64, 96, 128, 160, 256, 320, 400 }; /* default thumbnail size on startup (index in thumb_sizes[]): */ static const int THUMB_SIZE = 5; // starts at 256 px #endifCompile and install:
make sudo make installUse the New Thumbnail Sizes: In sxiv, sizes up to 400px are now available. Use
+to increase and-to decrease the size.
5. Summary of Keybindings
5.1 Ranger Shortcuts
| Action | Command |
|---|---|
| Start sxiv gallery in current directory | gG |
5.2 sxiv Gallery Mode Shortcuts
| Action | Command |
|---|---|
| Open single-image view | Enter |
| Mark an image (for multi-selection) | m |
| Zoom in | + |
| Zoom out | - |
| “Open with…” menu (interactive) | Ctrl-x o |
| Open ranger in a new terminal with file selected | Ctrl-x r |
5.3 sxiv Single-Image Mode Shortcuts
| Action | Command |
|---|---|
| Next image | n or Space |
| Previous image | p or Backspace |
| 10 images forward | ] |
| 10 images backward | [ |
| First image | g |
| Last image | G |
| Play/stop GIF animation | Ctrl-Space |
6. Debugging & Logs
All actions from the key-handler script are logged to /tmp/sxiv-open.log. There you can see:
- The key that was used (
ctrl-x rorctrl-x o). - The detected MIME type.
- The found
.desktopentries. - The chosen application and the executed command.
- The ranger call.
Example:
=== key-handler started Mon Apr 28 18:01:20 CEST 2025 ===
TERMWIN set to: xterm
Command argument: o
Processing file: '/home/.../Example.png'
MIME-Type: image/png
Found applications: gimp.desktop firefox.desktop ...
List file: /tmp/tmp.XXXXXX
Choice file: /tmp/tmp.YYYYYY
Selected: gimp.desktop
Desktop entry: /usr/share/applications/gimp.desktop
Exec command: gimp-3.0
Application started for '/home/.../Example.png'
=== key-handler finished ===
If something goes wrong, you can check the log:
less /tmp/sxiv-open.log


