Home Explore Blog CI



nushell

1st chunk of `cookbook/external_completers.md`
9d93a262fa358f735afa524a4627c212a241c0906899af240000000100000c96
---
title: External Completers
---

# External Completers

## Completers

### Carapace completer

```nu
let carapace_completer = {|spans|
    carapace $spans.0 nushell ...$spans | from json
}
```

### Fish completer

This completer will use [the fish shell](https://fishshell.com/) to handle completions. Fish handles out of the box completions for many popular tools and commands.

```nu
let fish_completer = {|spans|
    fish --command $"complete '--do-complete=($spans | str join ' ')'"
    | from tsv --flexible --noheaders --no-infer
    | rename value description
    | update value {
        if ($in | path exists) {$'"($in | str replace "\"" "\\\"" )"'} else {$in}
    }
}
```

A couple of things to note on this command:

- The fish completer will return lines of text, each one holding the `value` and `description` separated by a tab. The `description` can be missing, and in that case there won't be a tab after the `value`. If that happens, `from tsv` will fail, so we add the `--flexible` flag.
- 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.
- Since fish only supports POSIX style escapes for file paths (`file\ name.txt`, etc.), file paths completed by fish will not be quoted or escaped properly on external commands. Nushell does not parse POSIX escapes, so we need to do this conversion manually such as by testing if the items are valid paths as shown in the example. This simple approach is imperfect, but it should cover 99.9% of use cases.

### 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:

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

Title: External Completers: Carapace, Fish, Zoxide, and Multiple Completers
Summary
This section details how to use external programs to enhance Nushell's tab completion functionality. It provides examples for integrating completers for Carapace, Fish, and Zoxide. The Carapace completer leverages the `carapace` command-line tool. The Fish completer uses the Fish shell to provide completions. The Zoxide completer integrates with the Zoxide tool for quick directory navigation. The section also explains how to combine multiple completers using a `match` statement to handle different commands with specific completers while defaulting to a general one.