在 lf 中使用 Kitty 和 Tmux 显示图片
发布: (2026年1月3日 GMT+8 23:45)
3 min read
原文: Dev.to
Source: Dev.to
概览
在 lf(终端文件管理器)配合 Kitty 和 Tmux 使用时,切换预览图像会在屏幕上留下图形残影,使界面难以阅读。
解决方案依赖三个要素:
- lf 中的
redraw命令 - 不使用外部清理脚本
- lf 的
on-select事件
核心思路是让 lf 在每次选择时重新绘制自身,清空终端,从而保证预览显示干净。
配置 (lfrc)
在 ~/.config/lf/lfrc(或相应的 lfrc 位置)中添加以下内容:
set previewer ~/.local/bin/lfpreview
cmd on-select &{{
exec lf -remote 'send redraw'
}}
set previewer指定 lf 使用的预览脚本。on-select命令在每次选中文件时触发远程redraw请求。
预览脚本 (lfpreview)
创建文件 ~/.local/bin/lfpreview,写入以下内容并赋予可执行权限:
#!/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
工作原理
- 图片 – 直接使用 Kitty 的
icat显示。 - PDF – 使用
pdftoppm将第一页转换为图像后显示。 - 其他文件 – 用
less打开。
结尾的 exit 1 告诉 lf 不要 缓存预览。如果不加此行,刷新机制会复用缓存的预览,导致图像在每次选择时不重新绘制。
调整
此配置适用于典型使用场景,但你可能需要根据实际情况微调:
- 如果系统中
kitten icat或pdftoppm的路径不同,请相应修改。 - 在
case语句中添加其他 MIME 类型,以处理更多文件格式。
欢迎根据自己的工作流扩展脚本。