Skip to content

Instantly share code, notes, and snippets.

@MrPandir
Last active March 30, 2025 16:46
Show Gist options
  • Save MrPandir/bf46b9041cf2704233dfc65048acf24b to your computer and use it in GitHub Desktop.
Save MrPandir/bf46b9041cf2704233dfc65048acf24b to your computer and use it in GitHub Desktop.
My rewritten carapace configuration file for nushell.
use std "path add"
def cache [] {
let func = $in
let version = version | get version
let path = $nu.temp-path | path join "carapace-cache" $version
try {
open $path
} catch {
mkdir ($nu.temp-path | path join "carapace-cache")
do $func o> $path
open $path
}
}
def delete_pipe [spans: list<string>]: nothing -> list {
if $spans.0 == "|" {$spans | skip 1} else {$spans}
}
def expand_alias [spans: list<string>]: nothing -> list {
let expanded_alias = (
scope aliases
| where name == $spans.0
| get 0?.expansion?
)
if ($expanded_alias | is-empty) {
return $spans
}
$spans
| skip 1
| prepend ($expanded_alias | split words | first)
}
def get_carapace_complite [spans: list<string>] {
carapace $spans.0 nushell ...$spans | from json | default null
}
def ignore_error [] {
let input_data = $in
if ($input_data | is-empty) {
return null
}
if not (
$input_data
| get description?
| to text
| str contains "unknown shorthand flag"
) {$input_data}
}
def carapace_completer [] {{|spans|
let spans = delete_pipe $spans
let spans = expand_alias $spans
if ($spans | is-not-empty) {
get_carapace_complite $spans | ignore_error
}
}}
def get_config_dir []: nothing -> path {
$env.XDG_CONFIG_HOME? | default ($env.HOME | path join ".config")
}
export-env {
path add (get_config_dir | path join "carapace/bin")
if ($env.config?.completions?.external?.enable? | is-empty) {
$env.config.completions.external.enable = true
}
$env.config.completions.external.completer = (carapace_completer)
}
export def --env get-env [name] { $env | get $name }
export def --env set-env [name, value] { load-env { $name: $value } }
export def --env unset-env [name] { hide-env $name }
@MrPandir
Copy link
Author

MrPandir commented Jul 25, 2024

Different from the official compliter that is provided in the carapace _carapace command:

  1. Using the XDG_CONFIG_HOME variable (or HOME + .config) instead of the harcoded path.
  2. All built-in nushell commands are ignored.
  3. Pipe at the beginning is removed if it exists, it interfered with autocomplete.
  4. If you want to use an external command with autocomplete parameters, use env <name command>.
  5. The unknown shorthand flag error is ignored.
  6. More readable code, divided into functions.
  7. It's a module, not a script.

Examples

sort --u                  # will not produce anything, because there is no parameter with such a start.
env sort --u              # will complement to --unique because this parameter exists in the external command. 
(ls).name | env sort --re # it's also complemented to --reverse.
^sort --u                 # this will NOT autocomplete, as there is no way to detect this.
alias gp = git push
gp --                     # aliases will also work correctly.
carapace -/               # does not print the `unknown shorthand flag` error, instead it prints a nushell error.

Use

Warning

This script is written for version 0.103.0 and above.

  1. Download and save this script to a file
http get https://gist.githubusercontent.com/MrPandir/bf46b9041cf2704233dfc65048acf24b/raw/carapace.nu o> ($nu.data-dir | path join "modules" "carapace.nu")
  1. Make sure you have this path in NU_LIB_DIRS in the env.nu file so that you can import using only the file name
$env.NU_LIB_DIRS = [
    ($nu.default-config-dir)
    ($nu.data-dir | path join "modules") # here it is.
    ($nu.data-dir | path join "themes")
]
  1. And add a call to the end of your config.nu configuration
use carapace.nu *

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment