Skip to content

Add nushell completion for __zoxide_z#1129

Open
Juhan280 wants to merge 8 commits intoajeetdsouza:mainfrom
Juhan280:nushell_completion
Open

Add nushell completion for __zoxide_z#1129
Juhan280 wants to merge 8 commits intoajeetdsouza:mainfrom
Juhan280:nushell_completion

Conversation

@Juhan280
Copy link

@Juhan280 Juhan280 commented Nov 4, 2025

Nushell version 0.109.0 (nushell/nushell#15888) added support for completions to replace multiple arguments. This allows us to define a completion which replaces all the keywords with the correct file path upon accepting the path.

Also wrapped the whole script into a nushell module to prevent the completion command from polluting the command namespace. (That's why (the indentation) the diff is so messy, sorry)


Minimum nushell version required to make zoxide work without completion: 0.106.0
To make completion script work properly: 0.109.0

Copy link

@ysthakur ysthakur left a comment

Choose a reason for hiding this comment

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

Random Nushell user here. Thanks for this! I have some minor comments

def "nu-complete __zoxide_z" [context: string] {
let ast = ast --flatten $context | skip 1

# If the user has typed a space after the first argument, use the custom

Choose a reason for hiding this comment

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

I don't think $ast will be nonempty only if you add a space after the first argument. ast -f "z foo" (no space after foo) is still a length 2 list.

Suggested change
# If the user has typed a space after the first argument, use the custom
# If the user has entered an argument, use the custom

Copy link
Author

@Juhan280 Juhan280 Nov 13, 2025

Choose a reason for hiding this comment

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

If you look at the next few lines, I compare the output of ast with the length of the $context, thats how I tell if there is an extra space after the first arg or not.

    if $ast.0.span.end >= ($context | str length) { return null }

This prevents the completion command "nu-complete __zoxide_z" from
polluting the command namespace.
@Juhan280
Copy link
Author

Juhan280 commented Feb 12, 2026

Minimum nushell version required to make zoxide work without completion: 0.106.0
To make completion script work properly: 0.109.0

@ysthakur
Copy link

ysthakur commented Mar 5, 2026

@Juhan280 This works great, but if I have multiple arguments, the way the suggestions get replaced isn't super helpful. For example, if I type z / m, I might see suggestions for "~/reedline-main", among other paths. If I then choose "~/reedline-main", my buffer will be z / "~/reedline-main" rather than z "~/reedline-main".

This is really a limitation of Nushell, and we need to allow completers to replace arbitrary spans there. My closed PR handled this, but in a very hacky way.

edit: the above problem was just me being stupid, this code works perfectly fine


@ajeetdsouza Could we get this PR merged? Despite the problem mentioned above, I think it's good enough for completing single arguments (we can make a new PR in the future once Nushell's completion story improves). If it makes any difference, I'm one of the Nushell maintainers and can confirm that the code in this PR works.

@Juhan280
Copy link
Author

Juhan280 commented Mar 5, 2026

@ysthakur, what nushell version have you tried? You need 0.109.0 to replace arbitary span as I mentioned above.

@ysthakur
Copy link

ysthakur commented Mar 5, 2026

@Juhan280 I'm really sorry, I was just being really dumb above. I completely forgot that we already added the ability to specify spans in custom completers (I'm the one who implemented it).

I have 0.110 installed locally, but because of the shell.nix in the Zoxide repo, direnv was automatically switching to Nushell 0.104 in the Zoxide directory. I tried again with Nushell 0.110 and it works just fine. Sorry for the bother.

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.

2 participants