sxiv

来自 ArchWiki

sxiv,Simple X 图像查看器是一个用 C 语言编写的轻量级且可脚本化的图像查看器。

安装

安装 以下软件包之一

  • nsxivsxiv 的一个分支,现在已不再维护,目的是成为 sxiv 的(大部分)直接替代品,保持其界面并添加简单、明智的功能。安装 nsxiv-gitAUR 以获取开发版本。
  • sxiv,原始软件包。不再维护。

用法

分配键盘快捷键

sxiv 支持外部按键事件。首先,您必须按下 Ctrl-x 以将下一个按键发送到外部按键处理程序。外部按键处理程序需要一个可执行文件 ~/.config/sxiv/exec/key-handler,并通过参数传递按下的组合键,以及通过 stdin 传递当前标记的图像名称(或者,如果没有标记,则传递当前选定的图像)。安装 sxiv 时会附带一个示例按键处理程序:/usr/share/sxiv/exec/key-handler。该示例是自定义快捷键的绝佳起点。

$ mkdir -p ~/.config/sxiv/exec/
$ cp /usr/share/sxiv/exec/key-handler ~/.config/sxiv/exec/key-handler

请务必将脚本标记为可执行

在以下示例中,我们将添加绑定 Ctrl+d 以执行 mv filename ~/.trashCtrl+c 以使用 xclip 将当前图像的名称复制到剪贴板,以及 Ctrl+w 以使用 nitrogen 设置当前壁纸。显然,某些命令可能只对单个图像作为参数才有意义,因此您可能需要修改此设置以处理传递多个图像的情况。

~/.config/sxiv/exec/key-handler
#!/bin/sh
while read file
do
        case "$1" in
        "C-d")
                mv "$file" ~/.trash ;;
        "C-r")
                convert -rotate 90 "$file" "$file" ;;
        "C-c")
                echo -n "$file" | xclip -selection clipboard ;;
        "C-w")
                nitrogen --save --set-zoom-fill "$file" ;;
        esac
done

如果 .trash 文件夹不存在,则创建它

$ mkdir ~/.trash
提示: 您可能希望使用符合标准的回收站,而不是 mv "$2" ~/.trash

技巧和窍门

打开单个文件后浏览目录中的图像

sxiv 的开发者曾多次被要求使其程序浏览作为参数给出的文件名的目录中的所有图像(参见 [1][2])。有一些分支具有所需的功能:doronbehar'sqsmodo'ssammoth's

或者,您可以使用官方版本的 sxiv 并将 此脚本 放在 /usr/local/bin 中,并像这样调用它

$ scriptname a_single_image.jpg

如脚本注释中所示,从 ranger 中打开图像时,可以使用此行为。

来自 [3] 的这个 shell 脚本仅当传递的文件参数是文件夹时才以缩略图模式启动 sxiv

~/bin/sxiv.sh
#!/bin/sh

    if command -v sxiv >/dev/null 2>&1; then
      if [ -d "${@: -1}" ] || [ -h "${@: -1}" ]; then
        sxiv -t "$@"
      else
        sxiv    "$@"
      fi
    elif command -v feh >/dev/null 2>&1; then
      feh "$@"
    else
      echo "Please install SXIV or FEH!"
    fi

在状态栏中显示图像大小

将以下可执行脚本放在 ~/.config/sxiv/exec/image-info 中,并确保您已安装 exiv2 软件包

~/.config/sxiv/exec/image-info
#!/bin/sh

# Example for ~/.config/sxiv/exec/image-info
# Called by sxiv(1) whenever an image gets loaded,
# with the name of the image file as its first argument.
# The output is displayed in sxiv's status bar.

s=" | " # field separator

filename=$(basename "$1")
filesize=$(du -Hh "$1" | cut -f 1)

# The '[0]' stands for the first frame of a multi-frame file, e.g. gif.
geometry=$(identify -format '%wx%h' "$1[0]")

tags=$(exiv2 -q pr -pi "$1" | awk '$1~"Keywords" { printf("%s,", $4); }')
tags=${tags%,}

echo "${filesize}${s}${geometry}${tags:+$s}${tags}${s}${filename}"

参见