Nsxiv

2025-06-29

nsxiv

  • nsxiv - Neo (or New or Not) Simple (or Small or Suckless) X Image Viewer

Scripts

Open picture in GIMP on keypress

Pressing Ctrl-x in nsxiv calls $XDG_CONFIG_HOME/nsxiv/exec/key-handler with next keypress is passed as an argument, current image or marked images are sent on stdin.
My ~/.config/nsxiv/exec/key-handler, upon pressing g, open the image(s) in gimp:

#!/bin/sh
key="$1"

images=""
if [ "$NSXIV_USING_NULL" = "1" ]; then
    while IFS= read -r -d '' img; do
        images="$images \"$img\""
    done
else
    while IFS= read -r img; do
        images="$images \"$img\""
    done
fi

case "$key" in
    g)
        [ -n "$images" ] && eval gimp $images &
        ;;
    *)
        ;;
esac

Show all pictures in directory

When passed a directory, show all images from the directory in thumbnail mode: nsxiv.sh:

#!/bin/sh
last=""
for arg; do
    last="$arg"
done

if [ -d "$last" ] || [ -h "$last" ]; then
    nsxiv -t "$@"
else
    nsxiv "$@"
fi

Integration with ranger

When opening an image from ranger, I want to be able upon pressing enter to browse all images in the same directory.

nsxiv-ranger.sh opens nsxiv with list of all images in the same folder as current image and position of the current image:

#!/bin/bash
if [ $# -lt 1 ]; then
    echo "Usage: $0 <image-file>" >&2
    exit 1
fi

image=$1
if [ "$1" = "--" ]; then
    image=$2
fi

target=$(realpath -- "$image") || exit 1
dir=$(dirname -- "$target")

images=()
while IFS= read -r -d '' img; do
    images+=("$img")
done < <(find "$dir" -maxdepth 1 -type f \( \
    -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o \
    -iname '*.gif' -o -iname '*.bmp' -o -iname '*.webp' -o \
    -iname '*.tiff' -o -iname '*.svg' \) -print0 | sort -z)

index=1
found=0
for img in "${images[@]}"; do
    if [ "$img" = "$target" ]; then
        found=1
        break
    fi
    index=$((index + 1))
done

if [ "$found" -eq 0 ]; then
    echo "Image not found in sorted list: $target" >&2
    exit 1
fi

nsxiv -n "$index" "${images[@]}"

Use the script in ranger ~/.config/ranger/rifle.conf:

mime ^image, has nsxiv-ranger.sh,      X, flag f = nsxiv-ranger.sh -- "$@"