Skip to content

Instantly share code, notes, and snippets.

@onnyyonn
Last active July 23, 2022 06:04
Show Gist options
  • Save onnyyonn/d494cbd9acfb0353a347705052a0a33f to your computer and use it in GitHub Desktop.
Save onnyyonn/d494cbd9acfb0353a347705052a0a33f to your computer and use it in GitHub Desktop.

FZF Scripts

  • ksend: send files to other devices using kdeconnect
  • fz-open: navigate directory structure to find and open files
  • editdot: open dotfiles tracked by yadm in $EDITOR
  • dotlog: browse git log of dotfiles tracked by yadm, preview diff and copy commit hash
#!/bin/bash
# dotlog: interactively browse git log of dotfiles tracked by yadm, preview diff and copy commit hash
# requirements: yadm, fzf, xclip
yadm log --graph --format='%C(auto)%h%d %s %C(blue)%C(bold)%cr' --color=always | fzf --ansi --reverse --no-sort --preview 'yadm show --color=always $(echo {} | cut -c3-9)' | cut -c3-9 | tr -d $'\n' | xclip -selection clipboard
#!/bin/bash
# editdot: interactively open dotfiles tracked by yadm in $EDITOR
# requirements: yadm, fzf, bat, realpath
_fzf() {
command fzf "$@" --layout=reverse --info=inline --multi --height=80% --preview "bat --style=plain --color=always --theme='Nord' {}"
}
$EDITOR $(yadm list -a | sed "s/^/$(realpath ~ | sed 's/\/home\//\\\/home\\\//')\//" | _fzf)
#!/bin/bash
# fz-open: Navigate directory structure to find and open files
# requirements: fzf, fd, realpath
_fd() {
command fd "$@" --hidden --relative-path --search-path . --exclude '.git' --exclude 'node_modules' --exclude '.cache'
}
_fzf() {
command fzf "$@" --layout=reverse --info=inline --height=80%
}
get_parent_dirs_list() {
if [[ -d "${1}" ]]; then dirs+=("$1"); else return; fi
if [[ "${1}" == '/' ]]; then
for _dir in "${dirs[@]}"; do echo $_dir; done
else
get_parent_dirs_list $(dirname "$1")
fi
}
if test $# -gt 1; then
query=$2
parent_dir=$(realpath "$1")
elif test $# -eq 1; then
query=""
parent_dir=$(realpath "$1")
else
query=""
parent_dir=$(get_parent_dirs_list "$(realpath .)" | _fzf --prompt 'choose parent dir: ')
fi
out=("$(_fd . --type f --base-directory "$parent_dir" | cut -c3- | _fzf --prompt='Choose file: ' --query "$query" --print-query --expect=ctrl-u,ctrl-d,ctrl-e)")
query=$(head -1 <<< "$out")
key=$(head -2 <<< "$out" | tail -1)
file=$(head -3 <<< "$out" | tail -1)
while [[ "$key" = ctrl-u || "$key" = ctrl-d ]] ; do
if [ "$key" = ctrl-u ]; then
parent_dir=$(get_parent_dirs_list "$(realpath "$parent_dir")" | _fzf --prompt 'choose parent dir: ')
elif [ "$key" = ctrl-d ]; then
parent_dir="$parent_dir"/"$(_fd . --type d --base-directory "$parent_dir" --maxdepth 1 | cut -c3- | _fzf --prompt 'choose parent dir: ')"
fi
out=("$(_fd . --type f --base-directory "$parent_dir" | cut -c3- | _fzf --prompt='Choose file: ' --query "$query" --print-query --expect=ctrl-u,ctrl-d,ctrl-e)")
query=$(head -1 <<< "$out")
key=$(head -2 <<< "$out" | tail -1)
file=$(head -3 <<< "$out" | tail -1)
done
if [ -n "$file" ]; then
if [ "$key" = ctrl-e ]; then
$EDITOR "$(cd $parent_dir 2> /dev/null && pwd -P)"/"$file"
else
xdg-open "$(cd $parent_dir 2> /dev/null && pwd -P)"/"$file"
fi
fi
#!/bin/bash
# ksend: interactively send files to other devices from command-line, using kdeconnect
# requirements: kdeconnect-cli, fzf, fd
usage()
{
echo "Usage: ksend [{-v|--device} device] [{-d|--dir} directory] [{-n|--maxdepth} maxdepth]"
echo " ksend [-h|--help]"
}
help()
{
usage
echo
echo " {-v|--device} device -- Set destination device"
echo " If not set, it will be picked interactively from available devices"
echo " {-d|--dir} directory -- Set directory where the file is located (default: .)"
echo " {-n|--maxdepth} maxdepth -- Set maximum depth relative to the source directory, where search is restricted to (default: 1)"
echo " for unrestricted search, set value to inf (default: 1)"
echo " {-h|--help} -- Print this help message"
exit 0
}
#set default options
_fd() {
command fd "$@" --type f --hidden --unrestricted --relative-path --search-path . --exclude '.git' --exclude 'node_modules' --exclude '.cache'
};
_fzf() {
command fzf "$@" --layout=reverse --info=inline --multi --height=80%
};
# set the default args
dir="."; maxdepth=1;
flags()
{
while test $# -gt 0
do
case "$1" in
-d|--dir) dir="$2";;
-v|--device) device="$2";;
-n|--maxdepth) maxdepth="$2";;
-h|--help) help;;
-*|--*) echo "Unknown option $1"; exit 1;;
esac
shift
done
}
flags "$@"
if [ -z "$device" ]; then
device="$(kdeconnect-cli -a --name-only | _fzf --prompt='Choose device: ')"
fi
if [ "$maxdepth" == inf ]; then
file="$(cd $dir 2> /dev/null && pwd -P)"/"$(_fd . --base-directory $dir | cut -c3- | _fzf --prompt='Choose file: ')"; else
file="$(cd $dir 2> /dev/null && pwd -P)"/"$(_fd . --base-directory $dir --maxdepth $maxdepth | cut -c3- | _fzf --prompt='Choose file: ')"
fi
kdeconnect-cli -n "$device" --share "$file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment