Skip to content

Script for adding curent path to zoxide complete #1823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
TheCodeLamp opened this issue Feb 27, 2025 · 1 comment
Open

Script for adding curent path to zoxide complete #1823

TheCodeLamp opened this issue Feb 27, 2025 · 1 comment

Comments

@TheCodeLamp
Copy link

TheCodeLamp commented Feb 27, 2025

I like using zoxide as a replacement for cd, so I also like having the tab complete have the current dirs in the directory in addition to the zoxide complete.

I'm not too sure in my own nushell code, so I don't want to make a pull request, but I'd like to share how I added the current dir to my zoxide complete.

let zoxide_completer = {|spans|
  let path = ($spans.1 | glob $"\(?i\)($in)*" | where {path type | $in == dir} | each {path relative-to (glob "." | first)})
  let zoxide = ($spans | skip 1 | zoxide query -l ...$in | lines | where {|x| $x != $env.PWD})
  ($zoxide | prepend $path)
}

this addition to the completer allows you to do this:

Image

The first two entries are from the current dir, the second two are from zoxide.

There are a few places this script can fail. Firstly being with the path relative-to command. It will straight up fail if the prefix can't be found. I have an immutable distro so my /home/user folder is linked to the /var/home/user folder. This makes it unreliable to use $env.PWD as this will be different from what glob returns, so I just glob . instead, lol. Also, I'm unsure if spans always return more than one element. It seems to work with no arguments, but I don't know how it works.

I'm sure there are better ways of doing this and such, but it works for me. Just wanted to share if anyone wanted to refine it and add it to the external_completers.md file.

@TheCodeLamp
Copy link
Author

One thing I've noticed is that this setup can not handle paths with spaces. I have fixed this by wrapping paths containing spaces with backticks (`):

($zoxide | prepend $path | each {if ($in | str contains " ") { $"`($in)`" } else { $in }})

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

No branches or pull requests

1 participant