clipmenu

cli clipboard manager

clipmenu


#!/usr/bin/env bash

: "${CM_LAUNCHER=fzf}"

shopt -s nullglob

cache_dir=$(clipctl cache-dir)
cache_file=$cache_dir/line_cache

# Not -h, see #142
if [[  == --help ]]; then
    cat << 'EOF'
clipmenu is a simple clipboard manager using dmenu and xsel. Launch this
when you want to select a clip.

All arguments are passed through to dmenu itself.

Environment variables:

- $CM_DIR: specify the base directory to store the cache dir in (default: $XDG_RUNTIME_DIR, $TMPDIR, or /tmp)
- $CM_LAUNCHER: specify a launcher (default: fzf)
- $CM_OUTPUT_CLIP: if set, output clip selection to stdout
EOF
    exit 0
fi

if ! [[ -f "$cache_file" ]]; then
    printf '%s\n' 'No cache file yet, did you run clipmenud?'
    exit 2
fi

list_clips() {
    sort -rnk 1 < "$cache_file" | cut -d' ' -f2- | awk '!seen[{{&blob}}]++' | sed '/^.$/d'
}

chosen_line=$(list_clips | "$CM_LAUNCHER" "$@")
launcher_exit=$?

[[ $chosen_line ]] || exit 1
file=$cache_dir/$(cksum <<< "$chosen_line")
[[ -f "$file" ]] || exit 2

for selection in clipboard primary; do
    xsel --logfile /dev/null -i --"$selection" < "$file"
done

if (( CM_OUTPUT_CLIP )); then
    cat "$file"
fi

exit "${launcher_exit:-"$?"}"

Download

raw zip tar