Skip to content

Comments

Fix fzf recipe to handle header row and current worktree marker#118

Merged
k1LoW merged 1 commit intok1LoW:mainfrom
dgrant:fzf
Feb 8, 2026
Merged

Fix fzf recipe to handle header row and current worktree marker#118
k1LoW merged 1 commit intok1LoW:mainfrom
dgrant:fzf

Conversation

@dgrant
Copy link
Contributor

@dgrant dgrant commented Feb 8, 2026

Summary

  • Add --header-lines=1 so the table header is visible but not selectable in fzf
  • Fix awk to extract the path correctly when the current worktree (prefixed with *) is selected

Fixes #117

Test plan

  • Run cd $(git-wt | fzf --header-lines=1 | awk '{if ($1 == "*") print $2; else print $1}') in bash/zsh
  • Verify the header row is shown but cannot be selected
  • Select the current worktree (marked with *) and verify it cds to the correct path
  • Select a non-current worktree and verify it cds to the correct path

🤖 Generated with Claude Code

- Add --header-lines=1 so the table header is visible but not selectable
- Fix awk to extract the path correctly when the current worktree (prefixed
  with *) is selected

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Copilot AI review requested due to automatic review settings February 8, 2026 05:47
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the README’s fzf “interactive worktree selection” recipes to treat the first row as a non-selectable header and to correctly extract the worktree path when selecting the current worktree (marked with *).

Changes:

  • Add fzf --header-lines=1 to keep the header visible but not selectable.
  • Adjust awk to print the correct field when the selected row starts with the current-worktree marker *.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


``` console
$ cd $(git-wt | fzf | awk '{print $1}')
$ cd $(git-wt | fzf --header-lines=1 | awk '{if ($1 == "*") print $2; else print $1}')
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command substitution can expand to an empty string when the user cancels fzf (Esc/Ctrl-C). In that case cd is invoked with no argument and will take the user to $HOME, so this still doesn’t address the issue described in #117 even though the PR claims to fix it. Capture the selection into a variable and only run cd when it’s non-empty (and ideally check fzf’s exit status).

Copilot uses AI. Check for mistakes.

``` console
$ cd $(git-wt | fzf | awk '{print $1}')
$ cd $(git-wt | fzf --header-lines=1 | awk '{if ($1 == "*") print $2; else print $1}')
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bash/zsh one-liner is not safe for worktree paths that contain spaces (command substitution + awk + unquoted cd will word-split the path). Consider quoting the resulting path and/or using a selection format that’s unambiguous for paths with spaces.

Copilot uses AI. Check for mistakes.

``` console
$ cd (git-wt | fzf | awk '{print $1}')
$ cd (git-wt | fzf --header-lines=1 | awk '{if ($1 == "*") print $2; else print $1}')
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the bash/zsh recipe: if fzf is canceled, the substitution becomes empty and cd with no argument will switch to the home directory. Update this recipe to guard against an empty selection (and/or non-zero fzf exit status) before calling cd to truly resolve #117.

Copilot uses AI. Check for mistakes.

``` console
$ cd (git-wt | fzf | awk '{print $1}')
$ cd (git-wt | fzf --header-lines=1 | awk '{if ($1 == "*") print $2; else print $1}')
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fish, command substitution splits on whitespace, so a selected worktree path containing spaces would be split into multiple arguments and cd will fail. Prefer capturing the selection into a variable and then cd -- "$var" (or otherwise preserving spaces) to make the recipe robust.

Suggested change
$ cd (git-wt | fzf --header-lines=1 | awk '{if ($1 == "*") print $2; else print $1}')
$ set -l wt_dir (git-wt | fzf --header-lines=1 | awk '{if ($1 == "*") print $2; else print $1}')
$ cd -- "$wt_dir"

Copilot uses AI. Check for mistakes.
@k1LoW
Copy link
Owner

k1LoW commented Feb 8, 2026

Thank you for the good improvement!!

@k1LoW k1LoW merged commit 0f9398a into k1LoW:main Feb 8, 2026
8 of 9 checks passed
@github-actions github-actions bot mentioned this pull request Feb 8, 2026
@yermulnik
Copy link

yermulnik commented Feb 8, 2026

Coincidentally, yesterday I fiddled the Bash one-liner to match my prefs and conditions where the main reason for that was that every so often I use smaller terminal windows working with Git repos, which the fzf's defaults along with my local directories layout appeared to be not much of use as fzf doesn't allow horizontal scrolling and hence the paths get trimmed, while branch and commitish are hidden at all.
I didn't go with suggesting this as a PR to wt as it eventually turned into multi-line opinionated snippet which I wrapped in Bash shell function:

wt() {
        local __wtcdpath=$(
                git-wt | \
                fzf \
                        --cycle \
                        --wrap \
                        --layout=reverse \
                        --ghost=search \
                        --header-lines=1 \
                        --bind space:toggle-header \
                        --height=80% \
                        --preview 'echo {} | awk "{print \$(NF-1)}"' \
                        --preview-label=BRANCH \
                        --preview-label-pos=3 \
                        --preview-window=down:1 | \
                awk '{print $(NF-2)}'
        )
        [[ $__wtcdpath && $__wtcdpath != $PWD ]] && cd -- "$__wtcdpath"
}; export -f wt

What I see with default suggested command (incl this PR's improvement):
image

What I see with what I came up with:
image

Don't know whether it may make sense to add this to README, especially given I have no clue about non-Bash shells 🤷🏻 Let me know and I can PR it anyways though.

PS: It still is "vulnerable" to paths and branch names which contain whitespace chars as awk defaults to split by whitespace. The solution might be to add output separator feature so that the field separator can be redefined to something other than spaces (e.g. to a user-provided separator char OR provide predefined formats like default (what wt does at the moment), CSV, TSV, JSON, maybe even YAML or something else that can be easily parsed with common tooling).
Along with that it would be great to have an option to also reorder fields in the output, as I e.g. prefer the branch name to go first as I care about the branches rather than their paths inside my worktrees directory.
If these two sound like a good feature request, let me know and I'll file an issue for this. Thanks.

PPS: Ah, and the option to disable header row would be good too =)

@yermulnik
Copy link

@k1LoW @dgrant ⬆️

@k1LoW
Copy link
Owner

k1LoW commented Feb 8, 2026

Great recipe! I've created #122 as a place to collect community recipes like this — feel free to post it there.

@yermulnik
Copy link

I've created #122 as a place to collect community recipes like this — feel free to post it there.

Done 👍🏻

What do you think about the feature(s) request (custom output separator and/or pre-defined output formats, custom fields order, disable header row)? Thanks.

@yermulnik
Copy link

I went ahead and created #123. Thanks.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

minor bug: fzf suggestion in README.md doesn't work well if user hits ESC in fzf

3 participants