Skip to content
Adelhard Krämer edited this page Apr 12, 2022 · 13 revisions

Stand With Ukraine

fzy

Examples

Here are some scripts people have built using fzy. Please feel free to add your own.

Search for a file and open it in vim

FILE=$(ag -l -g '' | fzy) && vim "$FILE"

cd into a directory

This has to be a shell function (not script file) in order to use cd

fcd(){
  cd "$(find -type d | fzy)"
}

cd into a directory by keyword

Filter the directory to match the first keyword

f(){
  cd "$(find -type d -iname "*$1*" | fzy)"
}

Search for a gem and open a new tmux window in its directory

#!/bin/sh
GEM=$(bundle list | cut -f 4 -d' ' | fzy)
DESTINATION=$(bundle show $GEM)
tmux new-window -c "$DESTINATION" -n "$GEM"

Select some text to add to the clipboard

alias fzyank='fzy | xclip'

Zsh: Insert fuzzy-found paths directly into the shell command line

Based on snippet from selecta's EXAMPLES.md

function insert-fzy-path-in-command-line() {
        local selected_path
        echo # Run fzy underneath the current prompt
        selected_path=$(ag . -l -g '' | fzy) || return
        LBUFFER="$LBUFFER${(q)selected_path} " # ${(q)VAR} shell-escapes the string
        zle reset-prompt
}
zle -N insert-fzy-path-in-command-line

unsetopt flowcontrol # By default, ^S freezes terminal output, only needed if keybinding is ^S
bindkey "^S" "insert-fzy-path-in-command-line"

Zsh: Insert fuzzy-found paths directly into the shell command line(Not dependent on ag)

function history-fzy() {
  local tac

  if which tac > /dev/null; then
    tac="tac"
  else
    tac="tail -r"
  fi

  BUFFER=$(history -n 1 | eval $tac | fzy --query "$LBUFFER")
  CURSOR=$#BUFFER

  zle reset-prompt
}

zle -N history-fzy
bindkey '^r' history-fzy