post image January 11, 2022 | 2 min Read

How to preview images in Ranger file manager with iTerm and Tmux

I have been avare of ranger as a file system browser for quite some time.

Once you have installed ranger - go and edit following file ~/.config/ranger/rc.conf.

vim ~/.config/ranger/scope.conf
...
# How many columns are there, and what are their relative widths?
set column_ratios 1,2,5
...
set preview_images true
set use_preview_script true
set preview_images_method iterm2
...
:wq!

Then it’s importnat to setup html and json/terrafrom like files in ~/.config/ranger/scope.conf

vim  ~/.config/ranger/scope.conf
...

        ## HTML
        htm|html|xhtml)
            ## Preview as text conversion
            # w3m -dump "${FILE_PATH}" && exit 5
            # lynx -dump -- "${FILE_PATH}" && exit 5
            # elinks -dump "${FILE_PATH}" && exit 5
            pandoc -s -t markdown -- "${FILE_PATH}" && exit 5
            ;;

        ## JSON
        json|tfstate)
            jq --color-output . "${FILE_PATH}" && exit 5
            python -m json.tool -- "${FILE_PATH}" && exit 5
            ;;
        ...
        ...

        image/*)
            local orientation
            orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
            ## If orientation data is present and the image actually
            ## needs rotating ("1" means no rotation)...
            if [[ -n "$orientation" && "$orientation" != 1 ]]; then
                ## ...auto-rotate the image according to the EXIF data.
                convert -- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
                convert -- "${FILE_PATH}" -quality 25 -resize 1080 -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
            else
                convert -- "${FILE_PATH}" -quality 25 -resize 1080 "${IMAGE_CACHE_PATH}" && exit 6
            fi

            ## `w3mimgdisplay` will be called for all images (unless overriden
            ## as above), but might fail for unsupported types.
            exit 7;;

        ## Video
...
:wq!

And lastly I would recomend to edit ~/.config/ranger/rifle.conf file because html files were opened by elinks by default. I have changed it to vim editor.

Please add one line of code at the very begining in this file: ~/.config/ranger/rifle.conf

vim ~/.config/ranger/rifle.conf

...
ext x?html?, has vim,               terminal = ${VISUAL:-$EDITOR} -- "$@"
...
:wq!
author image

Jan Toth

I have been in DevOps related jobs for past 6 years dealing mainly with Kubernetes in AWS and on-premise as well. I spent quite a lot …

comments powered by Disqus