Ranger themes and rgrep

Fully customizable colorscheme and recursive grep support for the Ranger console file manager.

Themes for the ranger file manager typically undo, override, or map hardcoded values from the default colorscheme. This is hardly convenient and also unnecessarily restricts the expressiveness to a fraction of what would be possible.

The custom colorscheme declaratively exposes the internal context state while still using only the public interface for extensions. Apart from UI widgets, colors for LS_COLORS-like file types can be freely chosen (based on the basic mime-types). Per default, appearance and logic is intended to replicate the original scheme – also showing an overly colorful variant:

No installation is needed: The theme can simply be copied to ~/.config/ranger/colorschemes/custom.py and enabled with set colorscheme custom. See the ColorScheme class for changing preferences.

Note that the ranger interface itself only makes use of the mere 8 main colors, and the original theme sets bold together with bright for best compatibility. This color scheme implementation also adds standalone bright, underline, and dim as first class options – which recent terminal emulators should support. While otherwise sticking with 8+8 foreground/background colors, bold, and reverse, quite some more combinations become thus possible overall.

Ranger grep command

The ranger file manager allows to easily add custom commands using a Python interface. As (a possibly recursive) grep integration for searching file contents is missing, the ranger-commands.py extension adds the following related commands:

vcsstatus [state,…]
A filter_stack layer that evaluates a comma-separated list of VCS status flags. Possible values: conflict, untracked, deleted, changed, staged, ignored, sync, none, or unknown. If not given, filters for VCS state presence.
:flatten
Command that toggles “flat” view mode, which recursively crawls whole directories. Bases on the example in the ranger wiki.
:rfind [pattern]
Basically like flatten, but optionally post-filter using the given filename pattern. Files with the VCS ignore flag are filtered out, too.
:rgrep <pattern>
Calls grep on all files – that can be recursively loaded by flattening beforehand. Pattern matches are marked/highlighted for further processing or inspection.

No installation is needed: Simply copy to ~/.config/ranger/commands.py and invoke as usual.

Image and CSV preview

Ranger uses an external scope.sh script for creating file previews with the help of various commandline tools, which makes it easy to extend.

For ASCII art images, caca img2txt is suggested per default. However, just like the catimg alternative, this seems to have problems with passing through escape codes, for example resulting in blinking artifacts. When limiting the output to the 256 terminal colors (i.e., the standardized 240 color codes), using chafa seems to work.

handle_mime() {
    # …
    case "${mimetype}" in
        image/*)
            chafa -d 0 -f symbols -c 240 --fg-only --bg black \
                --preprocess on --dither diffusion --fill none --symbols all-wide-inverted-ascii-technical \
                -s "${PV_WIDTH}x${PV_HEIGHT}" -- "${FILE_PATH}" && exit 4
            exiftool "${FILE_PATH}" && exit 5
            exit 1;;
    esac
}

The column utility can create tables from CSV files. Separator characters are “auto-detected” by sniffing the first row.

handle_extension() {
    # …
    case "${FILE_EXTENSION_LOWER}" in
        csv)
            column --table --separator $(grep --only-matching --max-count 1 $'[\t;,]' -- "${FILE_PATH}" | head -n 1) \
                --output-separator '┆' --output-width "${PV_WIDTH}" --table-noheadings --table-noextreme 0 -- "${FILE_PATH}" && exit 4
            ;;
    esac
}

Code & Download