bitlux

fzf

Jaw-dropping moment today. I’m trying to find a better way to juggle an ever-increasing number of poorly-named Git branches. Towards that end, I installed and am trying out fzf. This example command was revelatory:

git branch | fzf --preview 'git show --color=always {-1}' | cut -c 3- | xargs git checkout

After some searching, I was able to write a function that does what I want:

gs() {
  if [ "$1" == "-" ]; then
    git switch -
  else
    git switch $(git branch | fzf -q $* --bind 'change:unbind(one),enter:become(echo {-1}),one:become(echo {-1})')
  fi
}

And a similar thing for cd:

f() {
  cd $(ls | fzf -q "$*" --bind 'change:unbind(one),enter:become(echo {-1}),one:become(echo {-1})')
}

I was so impressed, I donated to the author.