Home Explore Blog CI



nushell

4th chunk of `book/custom_completions.md`
ffa6a62bb22e73349603d3cfa3766f463efdbc225022a8640000000100000bb9
    remote?: string@"nu-complete git remotes",  # the name of the remote
    refspec?: string@"nu-complete git branches" # the branch / refspec
    ...
]
```

Custom completions will serve the same role in this example as in the previous examples. The examples above call into two different custom completions, based on the position the user is currently in.

## Custom Descriptions and Styles

As an alternative to returning a list of strings, a completion function can also return a list of records with a `value` field as well as optional `description` and `style` fields. The style can be one of the following:

- A string with the foreground color, either a hex code or a color name such as `yellow`. For a list of valid color names, see `ansi --list`.
- A record with the fields `fg` (foreground color), `bg` (background color), and `attr` (attributes such as underline and bold). This record is in the same format that `ansi --escape` accepts. See the [`ansi`](/commands/docs/ansi) command reference for a list of possible values for the `attr` field.
- The same record, but converted to a JSON string.

```nu
def my_commits [] {
    [
        { value: "5c2464", description: "Add .gitignore", style: red },
        # "attr: ub" => underlined and bolded
        { value: "f3a377", description: "Initial commit", style: { fg: green, bg: "#66078c", attr: ub } }
    ]
}
```

::: tip Note
With the following snippet:

```nu
def my-command [commit: string@my_commits] {
    print $commit
}
```

... be aware that, even though the completion menu will show you something like

```ansi
>_ my-command <TAB>
5c2464  Add .gitignore
f3a377  Initial commit
```

... only the value (i.e., "5c2464" or "f3a377") will be used in the command arguments!
:::

## External Completions

External completers can also be integrated, instead of relying solely on Nushell ones.

For this, set the `external_completer` field in `config.nu` to a [closure](types_of_data.md#closures) which will be evaluated if no Nushell completions were found.

```nu
$env.config.completions.external = {
    enable: true
    max_results: 100
    completer: $completer
}
```

You can configure the closure to run an external completer, such as [carapace](https://github.com/rsteube/carapace-bin).

An external completer is a function that takes the current command as a string list, and outputs a list of records with `value` and `description` keys, like custom completion functions. When the closure returns `null`, it defaults to file completion.

::: tip Note
This closure will accept the current command as a list. For example, typing `my-command --arg1 <tab>` will receive `[my-command --arg1 " "]`.
:::

This example will enable carapace external completions:

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

[More examples of custom completers can be found in the cookbook](../cookbook/external_completers.md).

Title: Customizing Completions with Descriptions, Styles, and External Completers
Summary
Nushell allows customizing completion suggestions with descriptions and styles by returning records with 'value', 'description', and 'style' fields. Additionally, external completers like Carapace can be integrated by setting the 'external_completer' field in the configuration to a closure that executes the external completer and transforms the output into the expected format. The closure receives the current command as a list of strings and should return a list of records with 'value' and 'description' keys. If the closure returns `null`, it defaults to file completion.