Skip to content

Instantly share code, notes, and snippets.

@orip
Last active September 5, 2022 11:19
Show Gist options
  • Save orip/16965728eb0d10f369b7a158d62dec74 to your computer and use it in GitHub Desktop.
Save orip/16965728eb0d10f369b7a158d62dec74 to your computer and use it in GitHub Desktop.
`wt` zsh function to switch between git worktrees using fzf
wt() {
if [ "$#" -eq 0 ]; then
local -a extra_fzf_args
elif [ "$#" -eq 1 ]; then
extra_fzf_args=(
--query
"'$1"
)
else
echo "Unknown arguments $@"
return 1
fi
# list worktrees, choose with fzf, split the output by whitespace into a zsh array
chosen=($(git worktree list | fzf -1 "$extra_fzf_args[@]"))
if [ $? -ne 0 ]; then
echo "Exiting"
return $?
fi
echo "Going to $chosen"
cd "$chosen[1]"
}
@orip
Copy link
Author

orip commented Sep 5, 2022

Demo:
demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment