How to complete rustup/cargo commands?
Asked Answered
H

4

8

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?

Hogwash answered 14/7, 2022 at 16:17 Comment(0)
H
22

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.

Hogwash answered 14/7, 2022 at 16:18 Comment(0)
R
5

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
Rear answered 20/4, 2023 at 5:39 Comment(0)
R
1

@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.

Rexanne answered 22/1, 2023 at 10:37 Comment(0)
R
0

If you are using Oh My Zsh, you can install the rust plugin to add completion for rustc, rustup, and cargo. To do so, add rust to the plugins in your .zshrc file:

plugins=(... rust)
Rawinsonde answered 3/11, 2023 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.