I have rustup+rust+cargo installed using the official installation script.
How do I enable shell completions, to be able to type cargo <TAB>
in the terminal and see the possible commands such as cargo check
?
I have rustup+rust+cargo installed using the official installation script.
How do I enable shell completions, to be able to type cargo <TAB>
in the terminal and see the possible commands such as cargo check
?
You can run rustup completions
and follow the instructions. For example, for bash
you can run
mkdir -p ~/.local/share/bash-completion/completions
rustup completions bash > ~/.local/share/bash-completion/completions/rustup
rustup completions bash cargo > ~/.local/share/bash-completion/completions/cargo
to generate completions for rustup
and cargo
respectively. Consult rustup completions
for further details, e.g. for configuring zsh
and fish
completions.
This will keep the completion functions up to date.
bash$ cat ~/.local/share/bash-completion/completions/rust
if type -P rustup > /dev/null; then
source <( rustup completions bash ) # for rustup
source <( rustup completions bash cargo ) # for cargo
fi
@AnonymousDuck's answer is a good way to do it in principle. A downside to "expanding" the completions is that one should remember to regenerate the expanded forms after updates, so they're kept up to date with the respective tools.
A way to make that happen on demand (sacrificing a few milliseconds on first invocation) is for example:
printf '. <(rustup completions bash)\n' >~/.local/share/bash-completion/completions/rustup
printf '. <(rustup completions bash cargo)\n' >~/.local/share/bash-completion/completions/cargo
A slight caveat that is rarely an issue is that the use of <(...)
requires the shell to not be in POSIX mode.
© 2022 - 2024 — McMap. All rights reserved.