Can't preview images in lf file manager with iterm2 under macOS

340 views Asked by At

I installed the lf file manager on macOS and configured it for my purposes but I can't configure it for image preview.

I tried it using ueberzugpp but it needs to add lfrun file to /usr/bin. It's not possible because it wants to remove system integrity protection.

I tried the w3mimgdisplay method described in lf github wiki. It also failed, but I can use imgcat to show images in iterm2.

Can I implement it to show images in lf file manager?

The ranger wiki page has iterm2 integration directly for image preview. It works in my ranger. But I want to use lf because I can run shell script in it easily.

1

There are 1 answers

0
Ortomala Lokni On

Start by configuring lf preview as explained in the man page by creating a ~/.config/lf/lfrc file:

set previewer ~/.config/lf/pv.sh
map i $~/.config/lf/pv.sh $f | less -R

and creating the ~/.config/lf/pv.sh script:

#!/bin/sh

case "$1" in
    *.tar*) tar tf "$1";;
    *.zip) unzip -l "$1";;
    *.rar) unrar l "$1";;
    *.7z) 7z l "$1";;
    *.pdf) pdftotext "$1" -;;
    ) highlight -O ansi "$1";;
esac

Set the script as executable with:

chmod +x ~/.config/lf/pv.sh

lf preview is now configured as in the man page. To add image preview, you can modify ~/.config/lf/lfrc such as:

set previewer ~/.config/lf/pv.sh
map i $~/.config/lf/pv.sh $f

and ~/.config/lf/pv.sh such as:

#!/bin/sh

alias imgcat=/Users/von/.iterm2/imgcat
case "$1" in
    *.tar*) tar tf "$1" | less -R;;
    *.zip) unzip -l "$1" | less -R;;
    *.rar) unrar l "$1" | less -R;;
    *.7z) 7z l "$1" | less -R;;
    *.pdf) pdftotext "$1" - | less -R;;
    *.jpg|*.jpeg) imgcat "$1" && read -n 1 -s -r;;
    *) highlight -O ansi "$1" | less -R;;
esac

This will allow you to preview jpeg files by pressing i in lf. You can easily add another image file formats in the preview script.

The main difference with the example in documentation is that the pager (less -R) is run directly in the preview script and only for text formats, as the pager cannot be run on images.