Home Explore Blog Models CI



nushell

2nd chunk of `cookbook/external_completers.md`
05052af50976a2ac81316e54f3df6b3e8cb12c28de9235710000000100000860
    $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:

```nu
let multiple_completers = {|spans|
    match $spans.0 {
        ls => $ls_completer
        git => $git_completer
        _ => $default_completer
    } | do $in $spans
}
```

> **Note**
> In the example above, `$spans.0` is the command being run at the time. The completer will match the desired completer, and fallback to `$default_completer`.
>
> - If we try to autocomplete `git <tab>`, `spans` will be `[git ""]`. `match $spans.0 { ... }` will return the `$git_completer`.
> - If we try to autocomplete `other_command <tab>`, `spans` will be `[other_command ""]`. The match will fallback to the default case (`_`) and return the `$default_completer`.

## Troubleshooting

### Alias completions

Nushell currently has a [bug where autocompletions won't work for aliases](https://github.com/nushell/nushell/issues/8483). This can be worked around adding the following snippet at the beginning of the completer:

```nu
# if the current command is an alias, get it's expansion
let expanded_alias = (scope aliases | where name == $spans.0 | get -i 0 | get -i expansion)

# overwrite
let spans = (if $expanded_alias != null  {
    # put the first word of the expanded alias first in the span
    $spans | skip 1 | prepend ($expanded_alias | split row " " | take 1)

Title: Zoxide and Multiple Completers, Troubleshooting Alias Completions
Summary
This section continues the discussion on external completers, focusing on the Zoxide and Multiple completers. It details how to create a completer for Zoxide, a tool for quickly navigating directories, and recommends its use as an override in a multiple completer setup. It then explains how to combine multiple external completers to handle different commands using a `match` statement. The section also addresses a known issue where autocompletions don't work for aliases in Nushell and provides a workaround to enable alias completions by expanding the alias and modifying the spans.