Skip to content

Instantly share code, notes, and snippets.

@junegunn
Last active March 19, 2026 01:29
Show Gist options
  • Select an option

  • Save junegunn/5f3f1056cc076113ad3bdd1e75bfde99 to your computer and use it in GitHub Desktop.

Select an option

Save junegunn/5f3f1056cc076113ad3bdd1e75bfde99 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
SCRIPT=${BASH_SOURCE}
POS_FILE=/tmp/fzf-demo-opener-pos
set_pos() {
echo "$1" > "$POS_FILE"
}
get_pos() {
if [[ -f $POS_FILE ]]; then
cat "$POS_FILE"
else
echo 0
fi
}
pos=$(get_pos)
if [[ $# -eq 0 ]]; then
export SCRIPT
set_pos $((pos - 1))
fzf --bind 'start,tab:transform-footer($SCRIPT next),shift-tab:transform-footer($SCRIPT prev),enter:execute:$SCRIPT -- {}' \
--bind 'click-footer:transform($SCRIPT click "$FZF_CLICK_FOOTER_WORD" {})'
exit $?
fi
ITEMS=(vim bat less)
COUNT=${#ITEMS[@]}
print_bar() {
local pos=$1
local out=""
for i in "${!ITEMS[@]}"; do
if ((i == pos)); then
out+=$'\e[7m'" ${ITEMS[$i]} "$'\e[0m'
else
out+=" ${ITEMS[$i]} "
fi
done
echo "$out"
}
action=$1
if [[ $action = click ]]; then
word="$2"
[[ -z $word ]] && exit
for i in "${!ITEMS[@]}"; do
if [[ "${ITEMS[$i]}" == "$word" ]]; then
pos=$i
break
fi
done
if [[ $FZF_KEY = double-click ]]; then
set_pos "$pos"
printf "execute:$SCRIPT -- %q" "$3"
else
set_pos $((pos - 1))
echo "transform-footer:$SCRIPT next"
fi
exit
fi
case "$action" in
next) ((pos = (pos + 1) % COUNT)) ;;
prev) ((pos = (pos - 1 + COUNT) % COUNT)) ;;
--)
case "${ITEMS[$pos]}" in
vim) vim "$2" ;;
bat) bat --paging always "$2" ;;
less) less "$2" ;;
esac
exit $?
;;
esac
set_pos "$pos"
print_bar "$pos"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment