Skip to content

Commit aae655f

Browse files
kiilclaudefdncred
authored
Document match_description option for custom completions (#2185)
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
1 parent b0eb327 commit aae655f

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

book/custom_completions.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ If you want to choose how your completions are filtered and sorted, you can also
4343
- `sort` - Set this to `false` to stop Nushell from sorting your completions. By default, this is `true`, and completions are sorted according to `$env.config.completions.sort`.
4444
- `case_sensitive` - Set to `true` for the custom completions to be matched case sensitively, `false` otherwise. Used for overriding `$env.config.completions.case_sensitive`.
4545
- `completion_algorithm` - Set this to `prefix`, `substring`, or `fuzzy` to choose how your completions are matched against the typed text. Used for overriding `$env.config.completions.algorithm`.
46+
- `match_description` - Set this to `true` to also match the typed text against each suggestion's description, in addition to its value. The inserted completion is still the suggestion's value. By default, this is `false`.
4647

4748
Here's an example demonstrating how to set these options:
4849

@@ -69,6 +70,27 @@ cat rat bat
6970

7071
Because we made matching case-insensitive, 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).
7172

73+
### Matching against descriptions
74+
75+
Custom completers can opt into matching the typed text against suggestion descriptions in addition to values, by setting `match_description: true` in the returned `options` record. The inserted completion is still the suggestion's value. This is useful when the value is an opaque identifier but the description is what the user is likely to type, such as completing an email address by the person's name:
76+
77+
```nu
78+
def "nu-complete users" [] {
79+
{
80+
options: {
81+
match_description: true,
82+
completion_algorithm: "substring",
83+
},
84+
completions: [
85+
{ value: "lk446763@example.com", description: "Lennart Kiil" },
86+
{ value: "ab123456@example.com", description: "Alice Bob" },
87+
]
88+
}
89+
}
90+
```
91+
92+
Now, typing `Lennart` and pressing the <kbd>Tab</kbd> key matches the description "Lennart Kiil" and inserts its value `lk446763@example.com`, even though the typed text doesn't appear in the value itself.
93+
7294
## Modules and Custom Completions
7395

7496
Since completion commands aren't meant to be called directly, it's common to define them in modules.

0 commit comments

Comments
 (0)