Estimated reading time: 3 minutes
Last updated on May 25th, 2025 at 05:47 pm
Table of Contents
Terraform Zsh Completion: Introduction
What is Terraform Zsh Completion?
Terraform is the most popular infrastructure as a code (IaC) to define and provision the infrastructure using the high-level configuration language. Zsh, on the other hand, is a powerful shell with many features built-in such as Terraform Zsh completion.
Terraform Zsh completion improves your development and devops workflow by providing intelligent suggestions and auto-completion for your terminal’s Terraform commands, variables, and resources.
Why Use Zsh Completion with Terraform?
Using Zsh completion with Terraform can significantly improve your productivity workflow by:
- Reducing Errors: Autocompletion helps prevent typos and syntax errors.
- Speeding Up Workflow: Quickly navigate through Terraform commands and options.
- Improve Usability: Get instant checks on valid commands and options, enhancing your command-line experience.
Prerequisites for Enabling Terraform Zsh Completion
Installing Zsh
Let’s get started by installing the Zsh, Here’s how to install Zsh on different operating systems:
On Ubuntu/Debian:
sudo apt update && sudo apt install zsh
On CentOS/RHEL:
sudo yum install zsh
On macOS (using Homebrew):
brew install zsh
After installation, you can verify it by running:
zsh --version
Installing Oh-My-Zsh
Oh-My-Zsh is an excellent open-source framework for managing your Zsh configuration. To install Oh-My-Zsh, execute the following commands:
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
This command downloads and runs the installation script. Once installed, Oh-My-Zsh will be set to the default shell.
Setting Up Terraform Zsh Completion
Installing Terraform
First, you need to install Terraform. Follow these steps based on your operating system:
On macOS (using Homebrew):
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
On Ubuntu/Debian:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
On Windows:
Download the Terraform binary from the official Terraform website and add it to your PATH.
Downloading Terraform Zsh Completion
Terraform provides Zsh completion:
terraform -install-autocomplete
Once the autocomplete support is installed, you will need to restart your shell or apply the changes by sourcing the .zshrc
file:
touch ~/.zshrc
Verifying Terraform Zsh Completion
Testing the Completion Setup
To verify that Zsh completion is working, start a new terminal session and type the following command:
terraform <TAB>
You should see a list of available Terraform commands and options.
If you encounter any issues, ensure the completion is correctly sourced in your .zshrc
.
Tips for Optimizing Zsh Completion
Some tips to enhance your Zsh completion with Terraform:
Custom Aliases and Functions: Create custom aliases or functions for frequently used Terraform commands. Add them to your .zshrc
file:
alias tf="terraform"
function tf-v() {
terraform validate $1
}
Additional Plugins: Consider installing other Zsh plugins to enhance productivity. Popular choices include zsh-syntax-highlighting
and zsh-autosuggestions
.
Conclusion
By enabling Terraform Zsh completion, you streamline your development process, reduce errors, and enhance productivity. The setup is straightforward, and the benefits are immediate.
Additional Reading