clipmenu

cli clipboard manager

clipmenu


#!/bin/bash

LC_COLLATE=C

dmenu_lines=${CLIPMENU_LINES-8}

declare -A selections
ordered_selections=()

files=("/tmp/clipmenu.$USER/"*)

for (( i=${#files[@]}-1; i>=0; i-- )); do
    file=${files[$i]}

    first_line=$(sed -n '/./{p;q}' "$file")

    lines=$(wc -l "$file")
    lines=${lines%% *}

    if (( lines > 1 )); then
        first_line+=" ($lines lines)"
    fi

    ordered_selections+=("$first_line")
    selections[$first_line]=$file
done

chosen_line=$(
    printf '%s\n' "${ordered_selections[@]}" |
        awk '!x[{{&blob}}]++' | dmenu -l "$dmenu_lines" "$@"
)

[[ $chosen_line ]] || exit 1

for selection in clipboard primary; do
    if type -p xsel >/dev/null 2>&1; then
        xsel --"$selection" < "${selections[$chosen_line]}"
    else
        xclip -sel "$selection" < "${selections[$chosen_line]}"
    fi
done

Download

raw zip tar