From 33770a7f05164d0f8bb1c2d2eb2a56dcfa735a1a Mon Sep 17 00:00:00 2001 From: zc he Date: Tue, 25 Mar 2025 20:43:22 +0800 Subject: [PATCH 1/3] remove zoxide completer from external_completers.md --- cookbook/external_completers.md | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/cookbook/external_completers.md b/cookbook/external_completers.md index e7128bea73..7054b4535a 100644 --- a/cookbook/external_completers.md +++ b/cookbook/external_completers.md @@ -32,36 +32,6 @@ A couple of things to note on this command: - The output of the fish completer does not contain a header (name of the columns), so we add `--noheaders` to prevent `from tsv` from treating the first row as headers and later give the columns their names using `rename`. - `--no-infer` is optional. `from tsv` will infer the data type of the result, so a numeric value like some git hashes will be inferred as a number. `--no-infer` will keep everything as a string. It doesn't make a difference in practice but it will print a more consistent output if the completer is ran on it's own. -### Zoxide completer - -[Zoxide](https://github.com/ajeetdsouza/zoxide) allows easily jumping between visited folders in the system. It's possible to autocomplete matching folders with this completer: - -```nu -let zoxide_completer = {|spans| - $spans | skip 1 | zoxide query -l ...$in | lines | where {|x| $x != $env.PWD} -} -``` - -This completer is not usable for almost every other command, so it's recommended to add it as an override in the [multiple completer](#multiple-completer): - -```nu -{ - z => $zoxide_completer - zi => $zoxide_completer -} -``` - -> **Note** -> Zoxide sets an alias (`z` by default) that calls the `__zoxide_z` function. -> If [alias completions](#alias-completions) are supported, the following snippet can be used instead: -> -> ```nu -> { -> __zoxide_z => $zoxide_completer -> __zoxide_zi => $zoxide_completer -> } -> ``` - ### Multiple completer Sometimes, a single external completer is not flexible enough. Luckily, as many as needed can be combined into a single one. The following example uses `$default_completer` for all commands except the ones explicitly defined in the record: @@ -154,8 +124,6 @@ let external_completer = {|spans| git => $fish_completer # carapace doesn't have completions for asdf asdf => $fish_completer - # use zoxide completions for zoxide commands - __zoxide_z | __zoxide_zi => $zoxide_completer _ => $carapace_completer } | do $in $spans } From 2a7a078201a30f7f5846a1316c73a969b7c95ffa Mon Sep 17 00:00:00 2001 From: zc he Date: Tue, 25 Mar 2025 20:54:17 +0800 Subject: [PATCH 2/3] Add zoxide completer to custom_completions.md --- book/custom_completions.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/book/custom_completions.md b/book/custom_completions.md index c5234d4b4c..1e4cb20b5b 100644 --- a/book/custom_completions.md +++ b/book/custom_completions.md @@ -71,6 +71,37 @@ cat rat bat Because we made matching case-insensitive and used `positional: false`, Nushell will find the substring "a" in all of the completion suggestions. Additionally, because we set `sort: false`, the completions will be left in their original order. This is useful if your completions are already sorted in a particular order unrelated to their text (e.g. by date). +## Another Practical Example - Zoxide Path Completions + +[Zoxide](https://github.com/ajeetdsouza/zoxide) allows easily jumping between visited folders in the system. It's possible to autocomplete matching folders with this completer: + +```nu +def "nu-complete zoxide path" [context: string] { + let parts = $context | split row " " | skip 1 + { + options: { + sort: false, + completion_algorithm: prefix, + positional: false, + case_sensitive: false, + }, + completions: (zoxide query --list --exclude $env.PWD -- ...$parts | lines), + } + } + +def --env --wrapped z [...rest: string@"nu-complete zoxide path"] { + let path = match $rest { + [] => { '~' } + ['-'] => { '-' } + [$arg] if ($arg | path type) == 'dir' => { $arg } + _ => { + zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n" + } + } + cd $path +} +``` + ## Modules and Custom Completions Since completion commands aren't meant to be called directly, it's common to define them in modules. @@ -230,4 +261,4 @@ let carapace_completer = {|spans| } ``` -[More examples of custom completers can be found in the cookbook](../cookbook/external_completers.md). +[More examples of external completers can be found in the cookbook](../cookbook/external_completers.md). From cff9743bef0d0c523c41d6ad54fe2f4135d6323e Mon Sep 17 00:00:00 2001 From: zc he Date: Wed, 26 Mar 2025 01:45:08 +0800 Subject: [PATCH 3/3] Update book/custom_completions.md Co-authored-by: Yash Thakur <45539777+ysthakur@users.noreply.github.com> --- book/custom_completions.md | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/book/custom_completions.md b/book/custom_completions.md index 1e4cb20b5b..eb8dd3b460 100644 --- a/book/custom_completions.md +++ b/book/custom_completions.md @@ -90,15 +90,7 @@ def "nu-complete zoxide path" [context: string] { } def --env --wrapped z [...rest: string@"nu-complete zoxide path"] { - let path = match $rest { - [] => { '~' } - ['-'] => { '-' } - [$arg] if ($arg | path type) == 'dir' => { $arg } - _ => { - zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n" - } - } - cd $path + __zoxide_z ...$rest } ```