# fzf.sh # Craig Jennings # FZF settings and utility functions # ============================================================================= # Settings # ============================================================================= export FZF_DEFAULT_OPTS='--height=70%' export FZF_DEFAULT_COMMAND='rg --files --no-ignore-vcs --hidden' export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND" # Directory paths for convenience functions IF_GAMES_DIR="$HOME/sync/org/text.games" BOOKS_DIR="$HOME/sync/books" # ============================================================================= # Navigation # ============================================================================= # cdff - change directory to where a file resides cdff() { file=$(fzf +m -q "$1") dir=$(dirname "$file") cd "$dir" || return 1 } # cdd - cd to directory with fzf cdd() { destdir=$(find "${1:-.}" -path '*/\.*' -prune \ -o -type d -print 2>/dev/null | fzf +m) && cd "$destdir" } # ============================================================================= # System Admin # ============================================================================= # Kill process with fzf kp() { pid=$(ps -ef | sed 1d | eval "fzf ${FZF_DEFAULT_OPTS} -m --header='[kill:process]'" | awk '{print $2}') if [ -n "$pid" ]; then echo "$pid" | xargs kill -"${1:-9}" kp fi } # Install packages with fzf preview yinstall() { yay -Slq | fzf --multi --preview 'yay -Si {1}' | xargs -ro yay -S --noconfirm } yinstall-skipverify() { yay -Slq | fzf --multi --preview 'yay -Si {1}' | xargs -ro yay -S --noconfirm --mflags --skipinteg } # Remove packages with fzf preview yrm() { yay -Qq | fzf --multi --preview 'yay -Qi {1}' | xargs -ro yay -Rns } # Find in file with fzf fif() { if [ "$#" -eq 0 ]; then echo "Need a string to search for!" return 1 fi rg --files-with-matches --no-messages "$1" | \ fzf --preview "highlight -O ansi -l {} 2>/dev/null | \ rg --colors 'match:bg:yellow' --ignore-case --pretty --context 10 '$1' || \ rg --ignore-case --pretty --context 10 '$1' {}" } # ============================================================================= # Convenience # ============================================================================= # Find and read epub book in terminal bk() { bkfile=$(find "$BOOKS_DIR" -iname "*.epub" -print | fzf) if [ -n "$bkfile" ]; then epr "$bkfile" fi } # Find and play interactive fiction game tg() { gamefile=$(find "$IF_GAMES_DIR" -type f \( -iname "*.z[1-8]" -o -iname "*.zblorb" -o -iname "*.blorb" \) -print | fzf) if [ -n "$gamefile" ]; then frotz "$gamefile" fi } # ============================================================================= # WireGuard # ============================================================================= wgup() { # Shutdown existing connections first output=$(sudo wg 2>/dev/null) if [ -n "$output" ]; then for iface in $(sudo wg show interfaces); do sudo wg-quick down "${iface}" done fi # Select and start new connection wgfile=$(sudo find /etc/wireguard/ -iname "*.conf" -exec basename -s .conf {} \; | fzf) if [ -n "$wgfile" ]; then sudo wg-quick up "$wgfile" sudo wg fi } wgdown() { output=$(sudo wg 2>/dev/null) if [ -n "$output" ]; then for iface in $(sudo wg show interfaces); do sudo wg-quick down "${iface}" done fi } alias wg=wgup