How to Enable Terraform Zsh Completion: Easy Guide

Estimated reading time: 3 minutes

Last updated on October 25th, 2024 at 03:10 pm

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.

Level Up Your DevOps Skills! 📈

Get Weekly Tips, Tutorials & Master the Latest Trends – Subscribe Now!

Subscribe Now!

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:
Bash
sudo apt update && sudo apt install zsh
On CentOS/RHEL:
Bash
sudo yum install zsh
On macOS (using Homebrew):
Bash
brew install zsh

After installation, you can verify it by running:

Bash
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:

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

DevOps Efficiency Hacks in Your Inbox! 📩

Stop wasting time searching. Get weekly tips & tutorials to streamline your DevOps workflow.

Subscribe Now!

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):
Bash
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
On Ubuntu/Debian:
Bash
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:

Bash
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:

Bash
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:

Bash
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:

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

Fast-Track Your DevOps Career 🚀

Stay ahead of the curve with the latest industry insights. Get weekly tips & propel your skills to the next level.

Subscribe Now!

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

Kashyap Merai
Kashyap Merai

Kashyap Merai, a Certified Solution Architect and Public Cloud Specialist with over 7 years in IT. He helped startups in Real Estate, Media Streaming, and On-Demand industries launch successful public cloud projects.

Passionate about Space, Science, and Computers, He also mentors aspiring cloud engineers, shaping the industry's future.

Connect with him on LinkedIn to stay updated on cloud innovations.