shell-history/scripts/hs

30 lines
1 KiB
Bash
Executable file

#!/bin/sh
# hs — search verbatim history with fzf and copy result to clipboard
copy_cmd=
case "$(uname -s)" in
Darwin) copy_cmd="pbcopy" ;;
*)
case "$XDG_SESSION_TYPE" in
x11) copy_cmd="xsel --clipboard" ;;
wayland) copy_cmd="wl-copy --trim-newline" ;;
*) echo "Session type not detected." >&2; exit 1 ;;
esac ;;
esac
entry="$(
verbatim search --compact --reverse --limit 10000 "$@" | \
fzf --ansi --disabled --query "${*:-}" \
--bind "start:reload:verbatim search --compact --reverse --limit 10000 {q}" \
--bind "change:reload:sleep 0.1; verbatim search --compact --reverse --limit 10000 {q} || true" \
--bind "ctrl-a:reload:verbatim search --compact --reverse {q}" \
--header "ctrl-a: search all"
)"
entry_cmd="$(printf '%s' "$entry" | sed 's/\x1b\[[0-9;]*m//g' | awk '{ print substr($0, index($0, $5)) }')"
if [ -n "$entry" ]; then
printf '%s' "$entry_cmd" | $copy_cmd >/dev/null
echo "Copied to clipboard." >&2
fi