Skip to main content
Enable Terraform Autocomplete in Bash, Zsh, and Fish

Enable Terraform Autocomplete in Bash, Zsh, and Fish

Key Takeaway

Enable Terraform tab autocomplete in Bash, Zsh, and Fish. One command setup, troubleshooting, and what gets auto-completed (subcommands, flags

Table of Contents

Quick Setup

One command enables autocomplete for your shell:

terraform -install-autocomplete

Then restart your shell:

exec $SHELL    # Or close and reopen your terminal

Now press Tab after typing terraform and subcommands auto-complete.

What Gets Auto-Completed

After enabling autocomplete:

terraform pl<Tab>        → terraform plan
terraform ap<Tab>        → terraform apply
terraform st<Tab>        → terraform state
terraform state l<Tab>   → terraform state list
terraform state s<Tab>   → terraform state show
terraform w<Tab>         → terraform workspace

Terraform autocomplete covers:

  • Subcommands: init, plan, apply, destroy, state, workspace, etc.
  • Flags: -auto-approve, -target, -var, -var-file, etc.
  • Sub-subcommands: state list, state show, state mv, workspace new, etc.

Setup by Shell

Bash

# Install autocomplete (adds to ~/.bashrc)
terraform -install-autocomplete

# Restart shell
exec bash

What it adds to ~/.bashrc:

complete -C /usr/local/bin/terraform terraform

This tells Bash to use the terraform binary itself for completion suggestions.

Manual install (if the auto-install doesn’t work):

echo 'complete -C /usr/local/bin/terraform terraform' >> ~/.bashrc
source ~/.bashrc

Replace /usr/local/bin/terraform with the actual path — find it with which terraform.

Zsh

# Install autocomplete (adds to ~/.zshrc)
terraform -install-autocomplete

# Restart shell
exec zsh

What it adds to ~/.zshrc:

autoload -U +X bashcompinit && bashcompinit
complete -C /usr/local/bin/terraform terraform

Manual install:

cat >> ~/.zshrc << 'EOF'
autoload -U +X bashcompinit && bashcompinit
complete -C /usr/local/bin/terraform terraform
EOF
source ~/.zshrc

Fish

Fish shell requires a different approach since it doesn’t use complete -C:

# Create the completion file
terraform -install-autocomplete

If that doesn’t work for Fish, install the community completions:

# Using Fisher
fisher install lgathy/google-cloud-sdk-fish-completion

# Or manually create completions
mkdir -p ~/.config/fish/completions
# Add terraform completions

macOS Notes

On macOS, Bash uses ~/.bash_profile instead of ~/.bashrc by default. If autocomplete doesn’t work:

# Check which file your shell loads
echo $SHELL

# For macOS Bash (before Catalina)
echo 'complete -C /usr/local/bin/terraform terraform' >> ~/.bash_profile
source ~/.bash_profile

# For macOS Zsh (Catalina and later, default shell)
terraform -install-autocomplete  # Writes to ~/.zshrc
exec zsh

Verify It Works

After installation:

terraform <Tab><Tab>

You should see a list of all Terraform subcommands:

apply         console       fmt           get           import
init          login         logout        output        plan
providers     refresh       show          state         taint
test          untaint       validate      version       workspace
destroy       force-unlock  graph         metadata      

Troubleshooting

Autocomplete doesn’t work after install

Check the terraform path:

which terraform
# /usr/local/bin/terraform

Make sure the path in your shell config matches:

grep terraform ~/.bashrc   # or ~/.zshrc
# complete -C /usr/local/bin/terraform terraform

If the paths don’t match, update the complete -C line.

“command not found: compdef” (Zsh)

Missing bashcompinit. Add this before the complete line:

autoload -U +X bashcompinit && bashcompinit

Already installed error

Error installing autocomplete: already installed in /home/user/.bashrc

Autocomplete is already set up. Just restart your shell:

exec $SHELL

Autocomplete stopped working after Terraform upgrade

The complete -C path may point to the old binary. Update it:

# Remove old entry
terraform -uninstall-autocomplete

# Reinstall
terraform -install-autocomplete
exec $SHELL

Doesn’t work in tmux/screen

Make sure your shell config file is sourced in non-login shells. Add to ~/.bashrc (not just ~/.bash_profile).

Uninstall Autocomplete

terraform -uninstall-autocomplete
exec $SHELL

Bonus: Alias Autocomplete

If you use a Terraform alias, add completion for it too:

# In ~/.bashrc or ~/.zshrc
alias tf="terraform"
complete -C /usr/local/bin/terraform tf

Now tf pl<Tab> completes to tf plan.

Hands-On Courses

Learn by doing with interactive courses on CopyPasteLearn:

Conclusion

Run terraform -install-autocomplete and restart your shell. That’s it. Works with Bash, Zsh, and Fish. If you use a tf alias, add complete -C $(which terraform) tf to your shell config. Tab completion saves time and prevents typos on every Terraform command.

🚀

Level Up Your Terraform Skills

Hands-on courses, books, and resources from Luca Berton

Luca Berton
Written by

Luca Berton

DevOps Engineer, AWS Partner, Terraform expert, and author. Creator of Ansible Pilot, Terraform Pilot, and CopyPasteLearn.