Displaying Images in lf with Kitty and Tmux
Source: Dev.to
Overview
When using lf (the terminal file manager) with Kitty and Tmux, image preview display can leave graphical artifacts on the screen when switching from one preview to another, making the interface unreadable.
The solution relies on three elements:
- The
redrawcommand in lf - No external cleaner script
- The
on-selectevent in lf
The key idea is to ask lf to redraw itself on each selection, which clears the terminal and ensures a clean preview display.
Configuration (lfrc)
Add the following to your ~/.config/lf/lfrc (or the appropriate lfrc location):
set previewer ~/.local/bin/lfpreview
cmd on-select &{{
exec lf -remote 'send redraw'
}}
set previewerpoints lf to the preview script.- The
on-selectcommand triggers a remoteredrawrequest each time a file is selected.
Preview Script (lfpreview)
Create the file ~/.local/bin/lfpreview with the following content and make it executable:
#!/bin/sh
draw() {
kitten icat --stdin no --transfer-mode memory \
--place "${w}x${h}@${x}x${y}" "$1" /dev/tty
}
file="$1"
w="$2"
h="$3"
x="$4"
y="$5"
case "$(file -Lb --mime-type "$file")" in
image/*)
draw "$file"
;;
application/pdf)
pdftoppm -f 1 -singlefile "$file" "/tmp/lfoutput"
draw "/tmp/lfoutput.ppm"
;;
*)
less "$1"
;;
esac
exit 1 # do not cache
chmod +x ~/.local/bin/lfpreview
How it works
- Images – displayed directly with Kitty’s
icat. - PDFs – the first page is converted to an image using
pdftoppm, then displayed. - Other files – opened with
less.
The exit 1 at the end tells lf not to cache the preview. Without this, the refresh mechanism would reuse cached previews, and the image would not be redrawn on each selection.
Adjustments
This configuration works for the typical use case, but you may need to tweak:
- Paths to
kitten icatorpdftoppmif they differ on your system. - Additional MIME types in the
casestatement to handle other file formats.
Feel free to extend the script to suit your workflow.