Created
March 30, 2026 04:29
-
-
Save scarf005/65674050b2f97cbd76f9947ab03b1056 to your computer and use it in GitHub Desktop.
put in ~/.config/fish/functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function wcd --description "Fuzzy-select a git worktree and cd into it" | |
| if not command -q fzf | |
| printf "wcd: fzf is required\n" >&2 | |
| return 1 | |
| end | |
| if not git rev-parse --is-inside-work-tree >/dev/null 2>/dev/null | |
| printf "wcd: not inside a git worktree\n" >&2 | |
| return 1 | |
| end | |
| set -l current (path resolve (git rev-parse --show-toplevel 2>/dev/null | string trim)) | |
| set -l records (git worktree list --porcelain -z | string split0) | |
| set -l entries | |
| set -l worktree_path | |
| set -l branch | |
| for record in $records | |
| if test -z "$record" | |
| if test -n "$worktree_path" | |
| set -l resolved_path (path resolve $worktree_path) | |
| set -l marker " " | |
| if test "$resolved_path" = "$current" | |
| set marker "*" | |
| end | |
| set -l name (path basename -- $worktree_path) | |
| if test -z "$branch" | |
| set branch "(detached)" | |
| end | |
| set -a entries (string join \t -- $worktree_path $marker $name $branch) | |
| end | |
| set worktree_path | |
| set branch | |
| continue | |
| end | |
| if string match -qr '^worktree ' -- $record | |
| set worktree_path (string replace -r '^worktree ' '' -- $record) | |
| else if string match -qr '^branch ' -- $record | |
| set branch (string replace -r '^branch refs/heads/' '' -- $record) | |
| end | |
| end | |
| if test (count $entries) -eq 0 | |
| printf "wcd: no worktrees found\n" >&2 | |
| return 1 | |
| end | |
| set -l query (string join ' ' -- $argv) | |
| set -l selected (printf '%s\n' $entries | fzf --delimiter \t --with-nth 2,3,4,1 --prompt 'worktree> ' --select-1 --exit-0 --query "$query") | |
| test -n "$selected" | |
| or return 130 | |
| set -l target (string split \t -- $selected)[1] | |
| cd -- $target | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment