Pulumi stack autocompletion
Pulumi is one of the best Infrastructure as Code (IaC) tools available. It allows you to manage your infrastructure using general-purpose programming languages, such as Python or Go.
Using autocomplete can save you a lot of time because you don’t have to type the entire command manually. By default, you can generate autocomplete functionality for Pulumi with the following command:
pulumi gen-completion bash
pulumi gen-completion zsh
pulumi gen-completion fish
While this command works well, it does not autocomplete stack names. For instance, if your project/stack structure looks like this:
-rw-r--r-- 1 user staff 58 14 Oct 2023 Pulumi.prod.yaml-rw-r--r-- 1 user staff 58 14 Oct 2023 Pulumi.staging.yaml-rw-r--r-- 1 user staff 58 14 Oct 2023 Pulumi.development.yaml-rw-r--r-- 1 user staff 65 14 Oct 2023 Pulumi.yaml-rw-r--r-- 1 user staff 4119 14 Oct 2023 go.mod-rw-r--r-- 1 user staff 29527 14 Oct 2023 go.sum-rw-r--r-- 1 user staff 2039 14 Oct 2023 main.go
When you run Pulumi with an explicitly defined stack name using the -s STACK_NAME
option, you must manually enter the stack name. However, you can easily add autocompletion for stack names by adding the following code to your shell profile:
__my_init_completion(){ COMPREPLY=() _get_comp_words_by_ref "$@" cur prev words cword}
_pulumi_custom_stack_completion() { local cur prev words cword
if declare -F _init_completion >/dev/null 2>&1; then _init_completion -s || return else __my_init_completion -n "=" || return fi
if [[ ${prev} == "-s" || ${prev} == "--stack" ]]; then COMPREPLY=( $(compgen -W "$(ls Pulumi.*.yaml 2>/dev/null | sed -E 's/Pulumi\.(.*)\.yaml/\1/')" -- "$cur") ) # If there's only one completion, remove the 'nospace' option if [[ ${#COMPREPLY[@]} -eq 1 ]]; then compopt +o nospace 2>/dev/null fi else __start_pulumi fi}
complete -o default -F _pulumi_custom_stack_completion pulumi
#compdef pulumi
_pulumi_custom_stack_completion() { local curcontext="$curcontext" state line typeset -A opt_args
_arguments -s -S \ '(-s --stack)'{-s,--stack}'[specify stack]:stack name:->stacks' \ '*:: :->args'
case $state in stacks) local -a stack_list stack_list=($(ls Pulumi.*.yaml 2>/dev/null | sed -E 's/Pulumi\.(.*)\.yaml/\1/')) _describe -t stacks 'stack names' stack_list ;; *) # Fall back to default Pulumi completion if available _pulumi ;; esac}
_pulumi_custom_stack_completion "$@"
function _pulumi_stack_completion # Get the current word being completed set -l cur (commandline -t)
# Complete only for -s or --stack flags set -l prev (commandline -opc)[-1] if contains -- $prev "-s" "--stack" # List Pulumi stacks from yaml files for file in Pulumi.*.yaml if test -f "$file" # Extract stack name by removing Pulumi. prefix and .yaml suffix set -l stack (string replace -r '^Pulumi\.(.*)\.yaml$' '$1' $file) echo $stack end end else # Default Pulumi options complete -C "pulumi " # Triggers Pulumi's built-in completion if available endend
complete -c pulumi -f # -f prevents filename completioncomplete -c pulumi -s s -l stack -a "(_pulumi_stack_completion)" -d "Select a stack"
After updating your profile, the shell will suggest the correct stack name after you type -s
, rather than offering a file name.
© 2025 Aleksandr Aksenov. All Rights Reserved.