Enable autocomplete for shell
4 minute read
If you installed the Score implementation (CLI) by downloading the single binary, continue reading.
Overview
Score allows you to generate an autocompletion script for each Score implementation tool for a specified shell.
Score supports autocompletion of commands in a few shells.
Use cases
- Press the tab key when a command or flag is partially typed and there is one option to complete.
- Press the tab key when there are multiple candidates and allow you to continue typing.
Available shells
The following are available shells.
Shell | Description |
---|---|
bash | Generates the autocompletion script for bash. |
fish | Generates the autocompletion script for fish. |
powershell | Generates the autocompletion script for powershell. |
zsh | Generates the autocompletion script for zsh. |
Enable shell autocompletion
Score provides autocompletion support for Bash, Zsh, Fish, and PowerShell which can save you a lot of typing.
Below are the procedures to set up autocompletion for Bash, Fish, and Zsh.
The Score completion script for Bash can be generated with score-<platform> completion bash
.
Sourcing this script in your shell enables Score completion.
Prerequisites
The Score completion script depends on bash-completion package.
Use Homebrew to install:
brew install bash-completion
Install Bash
Install Bash through Homebrew.
brew install bash
Reload your shell and verify that the desired version is being used:
echo $BASH_VERSION $SHELL
Homebrew usually installs it at /usr/local/bin/bash
.
Enable Score autocompletion
You now have to ensure that the Score completion script gets sourced in all your shell sessions. There are multiple ways to achieve this:
-
Source the completion script in your
~/.bash_profile
file:echo 'source <(score-<platform> completion bash)' >>~/.bash_profile
Replace
<platform>
with the name of the Score implementation (CLI), for example:echo 'source <(score-humanitec completion bash)' >>~/.bash_profile
echo 'source <(score-compose completion bash)' >>~/.bash_profile
echo 'source <(score-helm completion bash)' >>~/.bash_profile
-
Add the completion script to the
/usr/local/etc/bash_completion.d
directory:score-<platform> completion bash >/usr/local/etc/bash_completion.d/score
-
If you have an alias for Score, you can extend shell completion to work with that alias:
echo 'alias k=Score' >>~/.bash_profile echo 'complete -o default -F __start_score-<platform> k' >>~/.bash_profile
Replace
score-<platform>
with the name of the Score implementation (CLI) you want to use.
The Score completion script for Fish can be generated with the command score-<platform> completion fish
.
Sourcing the completion script in your shell enables Score autocompletion.
To do so in all your shell sessions, add the following line to your ~/.config/fish/config.fish
file:
score-<platform> completion fish | source
Replace score-<platform>
with the name of the Score implementation (CLI) you want to use, for example:
score-humanitec completion fish | source
After reloading your shell, Score autocompletion should be working.
The Score completion script for Zsh can be generated with the command score-<platform> completion zsh
. Sourcing the completion script in your shell enables Score autocompletion.
To do so in all your shell sessions, add the following to your ~/.zshrc
file:
- Open your
.zshrc
file. The following step usesnano
to open.
nano ~/.zshrc
- Source the completion script in your shell. Replace
<platform>
with the platform of your choice.
# adding score-<platform>
source <(score-<platform> completion zsh)
Replace score-<platform>
with the name of the Score implementation (CLI) you want to use, for example:
source <(score-humanitec completion zsh)
If you have an alias for Score, Score autocompletion will automatically work with it.
After reloading your shell, Score autocompletion should be working.
If you get an error like
2: command not found: compdef
, then add the following to the beginning of your~/.zshrc
file to initialize the completion for the current session.
autoload -Uz compinit
compinit
The Score completion script for PowerShell can be generated with the command score-<platform> completion powershell
.
Sourcing the completion script in your shell enables Score autocompletion.
- To do so in all your shell sessions, add the following line to your
$PROFILE
file.
score-<platform> completion powershell | Out-String | Invoke-Expression
Replace score-<platform>
with the name of the Score implementation (CLI) you want to use, for example:
score-humanitec completion powershell | Out-String | Invoke-Expression
This command will regenerate the auto-completion script on every PowerShell start up. You can also add the generated script directly to your $PROFILE
file.
- To add the generated script to your
$PROFILE
file, run the following line in your PowerShell prompt.
score-<platform> completion powershell >> $PROFILE
After reloading your shell, Score autocompletion should be working.
For help with enabling autocomplete script for the shell, use the --help
flag on the specified shell, for example:
score-compose completion bash --help
For more information, see Cobra’s documentation on Generating shell completions.