2

I'm not a fan of the default preview for archives in Ranger, and I want to change it.

As an example, for .zip files, the preview displays the plain output of unzip -l <file>. Using custom commands however, it's possible to display a tree view of the archive (as an example, .zip files can be viewed as a tree with unzip -l <file> | awk 'FNR>3 {$1=$2=$3=""; print substr($0,4)}' | tree --fromfile.

How do I configure Ranger to preview/display the archive file with a custom shell command for each filetype (.tar.gz, .zip, .7z, etc...)?


Related: How do I get a better exploration for archives in ranger?

1 Answers1

2

The preview is defined in .config/ranger/scope.sh. If it does not exist in you configuration, run ranger --copy-config=scope in your shell, and it'll pop up in your .config folder.

After that, you can change the scope.sh file as you please. In the specific examples of archives, you can change the handle_extension function.

Here are some examples that will display .tar.gz, .zip, and .7z files as trees in the preview:

tar|gz)
    tar -tf "${FILE_PATH}" | tree --fromfile && exit 5
    exit 1;;
zip)
    unzip -l -- "${FILE_PATH}" | awk 'FNR>3 {$1=$2=$3=""; print substr($0,4)}' | tree --fromfile && exit 5
    exit 1;;
7z)
    # This awk script finds out which character column "Name" is at.
    awk_script='{if (ix == 0) {ix = index($0, "Name");}p = (body == 1);if (ix > 0) {body = (body + ($0 ~ / *-[ -]+/)) % 2;}if (p == 1 && body == 1) {print ix;}}'
    7z l "${FILE_PATH}" | awk 'FNR>20 {print $0}' | head -n -2 | cut -c$(7z l "${FILE_PATH}" | awk "$awk_script")- | tree --fromfile && exit 5
    exit 1;;