worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

[Question]: Is there a way to autocomplete names?

Open omerxx opened this issue 9 months ago • 11 comments

I'd be happy to: wt switch <TAB> and see an autocompletion options pulled automatically, or, better yet -> sent to fzf to make things ever smoother.

Is there a way to achieve either?

Context: I did try to wt list --output json and port that to jq, then xargs but for whatever reason (probably has to do my Nushell setup) it's not working great. Would love to have a native option.

omerxx avatar Feb 26 '26 12:02 omerxx

Thanks for reaching out! This looks like a usage question rather than a bug report.

wt has built-in shell completions — wt switch <TAB> should show branch names (with worktree status and recency info) once shell integration is set up. Since you're on Nushell, the easiest way is:

wt config shell install

This writes the integration automatically. Or for manual setup:

wt config shell init nu | save -f ($nu.default-config-dir | path join vendor/autoload/wt.nu)

After restarting your shell, wt switch <TAB> should offer branch completions. The completions are dynamic (they call the binary at tab-time), so they'll always reflect your current branches.

Regarding wt list --format=json with jq — that should also work for scripting, though the native completions above are likely what you're after for interactive use.

A maintainer can provide more detail if needed.

worktrunk-bot avatar Feb 26 '26 12:02 worktrunk-bot

to confirm, does autocomplete work for you in shells other than nushell? it should do

then I'll look into nushell; I have a faint memory of there being a reason it wasn't easy to provide this

max-sixty avatar Feb 26 '26 12:02 max-sixty

Hey @max-sixty, thanks! TBH, the issue even with the one-liner the bot provided above, is nushell not recognizing any output through the pipe. The output is definitely printed from wt config shell init nu and wt list --format=json but the pipe sends nothing. Not sure about it (other CLIs don't have an issue).

Anyway, I know nu is a bit of a niche. It's fine, though I was missing somehting. Unless this is of interest to others I'm fine with closing it, I'll try to work it out myself locally someone.

Thanks

omerxx avatar Feb 26 '26 21:02 omerxx

I thought we had fixed that issue re stdout — would you mind checking wt --version?

max-sixty avatar Feb 27 '26 03:02 max-sixty

Just updated to latest wt 0.28.0. Still seeing the broken pipe between wt list --output json and | jq

I'll try to explore this with claude and let you know if I find something useful.

omerxx avatar Feb 27 '26 09:02 omerxx

The broken pipe fix (#1081) shipped in v0.26.0, but it lives in the shell wrapper script, not the binary. Updating wt alone won't pick it up — the wrapper installed in your Nushell config needs to be regenerated:

wt config shell install

You can verify the fix is in place by checking whether your wrapper has the list passthrough path (the if ... ($args | first) == "list" branch). The installed wrapper location is typically $nu.default-config-dir/vendor/autoload/wt.nu.

Also a small note: the flag is --format json, not --output json — though if --output were unrecognized, clap would error rather than silently misbehave, so that may just be a typo in the comment.

worktrunk-bot avatar Feb 27 '26 09:02 worktrunk-bot

The broken pipe fix (#1081) shipped in v0.26.0, but it lives in the shell wrapper script, not the binary. Updating wt alone won't pick it up — the wrapper installed in your Nushell config needs to be regenerated:

wt config shell install

You can verify the fix is in place by checking whether your wrapper has the list passthrough path (the if ... ($args | first) == "list" branch). The installed wrapper location is typically $nu.default-config-dir/vendor/autoload/wt.nu.

Also a small note: the flag is --format json, not --output json — though if --output were unrecognized, clap would error rather than silently misbehave, so that may just be a typo in the comment.

Yep, my mistake with the flag, I used the correct one.

I can confirm updating the init script fixed the piped output to jq 💪 However, <tab> completion for switching worktrees still won't work, and for whatever reason, trying to run xargs to switch fails too:

echo "strava-integration" | xargs -I {} wt switch {}
▲ Worktree for strava-integration @ ~/fatbot.strava-integration, but cannot change directory — shell integration not installed

(for context wt switch strava-integration works flawlessly.)

omerxx avatar Feb 27 '26 09:02 omerxx

Good news on the piping fix!

Two things on the remaining issues:

Tab completion: This is a known gap — nushell completions aren't wired up yet. The wrapper script handles cd directives and pipe passthrough, but doesn't include completion registration (contrast with bash/zsh which register a lazy completer via COMPLETE=$SHELL wt). Nushell's completion system uses extern blocks, and adding support is tracked as part of the experimental nushell work. @max-sixty would know best whether this is something planned for a near-term release.

xargs with wt switch: This is a fundamental shell limitation, not nushell-specific. wt switch works by having the shell wrapper cd in the current shell process. xargs spawns a subprocess, so even if the wrapper ran there, the cd would only affect that subprocess — your parent shell stays put. The "shell integration not installed" message is accurate: the binary is running directly (not through the wrapper) and can't change your directory.

If you need scripted switching, you can grab the path and cd yourself:

cd (wt list --format json | from json | where branch == "strava-integration" | get path.0)

worktrunk-bot avatar Feb 27 '26 09:02 worktrunk-bot

Two separate things going on here:

Tab completions: Nushell completions aren't implemented yet — the shell wrapper provides the def --env --wrapped wt function for directory-changing, but no extern definitions with completion generators. That's why wt switch <TAB> doesn't offer suggestions. This would need dedicated work to add.

xargs behavior: This is expected. xargs spawns a child process that calls the wt binary directly, bypassing the shell wrapper function. Since wt switch needs the wrapper to cd in your shell (via the directive file mechanism), calling it from xargs means there's no wrapper to process the cd directive — hence the "shell integration not installed" message. Even if it could detect the wrapper, a subprocess can't change the parent shell's directory. This same limitation applies in all shells, not just nushell.

For scripting workarounds, you could grab the path and cd yourself:

let path = (wt list --format json | from json | where branch == "strava-integration" | get path | first)
cd $path

worktrunk-bot avatar Feb 27 '26 09:02 worktrunk-bot

Good to hear the pipe fix worked!

Tab completion: This is a real gap — the Nushell wrapper currently doesn't include completion registration, unlike the bash and zsh templates which set up lazy completion via COMPLETE=$SHELL wt. The completion engine does support Nushell (branch names, flags, etc. all work via COMPLETE=nu), but the nushell.nu template doesn't wire it up. I'll look into adding Nushell completion registration to the wrapper.

xargs issue: This one is expected, unfortunately. wt switch needs to change your current shell's directory — that's what the wrapper function does (it reads the cd directive from a temp file and runs it in your shell session). When you run wt switch via xargs, it spawns the raw binary in a subprocess, bypassing the wrapper entirely. The subprocess can't change the parent shell's directory, hence the "shell integration not installed" message.

For scripting worktree switches in Nushell, you can call the wrapper function directly:

let branch = "strava-integration"
wt switch $branch

This works because Nushell evaluates wt as the wrapper function (not the binary), so the cd directive gets processed in your session.

worktrunk-bot avatar Feb 27 '26 09:02 worktrunk-bot

Makes sense. xargs calls a sub process, should've expected that. anyway, we've diverted here, I'll try to propose nushell completion on a separate thread, thanks @max-sixty and @worktrunk-bot :)

omerxx avatar Feb 27 '26 09:02 omerxx