> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Making the Most of the Terraform CLI

> Guide to Terraform CLI productivity features, enabling autocomplete, using built-in help, and formatting configurations with terraform fmt to improve speed, accuracy, and consistent style.

Terraform includes several built-in CLI conveniences—autocomplete, comprehensive help, and automatic formatting—that help you stay productive without leaving your terminal. This guide walks through enabling and using these features so you can discover commands, reduce typos, and keep configuration style consistent.

## Autocomplete: speed up typing and reduce errors

Shell autocomplete lets your shell predict and complete Terraform commands, subcommands, flags, and file paths as you type. If you already have shell completion enabled, this is a quick review. If not, enabling it consolidates useful CLI ergonomics in one place.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Terraform-CLI/Making-the-Most-of-the-Terraform-CLI/terraform-cli-autocomplete-promotion.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=b75044e2a2f9005627a57f57dab5b40f" alt="The image is a promotional graphic for using auto-complete with the Terraform CLI, explaining that tab completion aids in filling subcommands, flags, and file paths by pressing the Tab key. It features geometric lines and logos on a dark background." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Terraform-CLI/Making-the-Most-of-the-Terraform-CLI/terraform-cli-autocomplete-promotion.jpg" />
</Frame>

Install autocomplete for your shell with:

```bash theme={null}
$ terraform -install-autocomplete
```

If your shell configuration file does not exist, create it first (example for bash):

```bash theme={null}
$ touch ~/.bashrc
$ terraform -install-autocomplete
```

<Callout icon="lightbulb" color="#1CB2FE">
  After installing autocomplete, reload your shell configuration (for example, `source ~/.bashrc` or `source ~/.zshrc`) or restart the terminal to activate completion.
</Callout>

Usage example: type a partial command then press Tab. Typing `terraform s` and pressing Tab will present matching subcommands:

```bash theme={null}
$ terraform s<TAB>
show    state
```

Autocomplete is especially helpful for remembering long flag names, provider-specific subcommands, and relative file paths.

## Built-in help: discover commands and flags

Terraform ships contextual help at both the global and subcommand levels. Use `--help` to view available commands, flags, and short explanations without leaving your terminal.

Global overview:

```bash theme={null}
$ terraform --help
Usage: terraform [global options] <subcommand> [args]

The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.

Main commands:
  init      Prepare your working directory for other commands
  validate  Check whether the configuration is valid
  plan      Show changes required by the current configuration
  apply     Create or update infrastructure
  destroy   Destroy previously-created infrastructure
  ...
```

Per-command help provides detailed flag descriptions and usage examples. For example:

```bash theme={null}
$ terraform plan --help
Usage: terraform [global options] plan [options]

Generates a speculative execution plan, showing what actions Terraform
would take to apply the current configuration. This command will not
actually perform the planned actions.

You can optionally save the plan to a file, which you can then pass to
the "apply" command to perform exactly the actions described in the plan.

Plan Customization Options:
  -destroy        Select the "destroy" planning mode, which creates a plan
                  to destroy all objects currently managed by this
                  configuration.
  ...
```

Use per-command help whenever you need to confirm a flag name, understand an option’s effect, or find switches for advanced behavior.

<Callout icon="lightbulb" color="#1CB2FE">
  For extended guides, examples, and tutorials, see the official Terraform documentation: [developer.hashicorp.com/terraform](https://developer.hashicorp.com/terraform).
</Callout>

## Formatting with terraform fmt

Consistent formatting improves readability and reduces diffs in commits. `terraform fmt` rewrites Terraform configuration files to the canonical style.

Example of a compact resource before formatting:

```hcl theme={null}
resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  tags = { Name = "example" }
}
```

Run `terraform fmt` in the working directory:

```bash theme={null}
$ terraform fmt
```

To format files in subdirectories as well, use the recursive flag:

```bash theme={null}
$ terraform fmt -recursive
```

Notes:

* By default, `terraform fmt` processes files in the current directory only.
* Add `-recursive` to traverse and format nested directories.

## Quick command reference

|                           Command | Purpose                                                | Example                           |
| --------------------------------: | ------------------------------------------------------ | --------------------------------- |
| `terraform -install-autocomplete` | Enable shell autocomplete for Terraform                | `terraform -install-autocomplete` |
|                `terraform --help` | Show global command list and usage                     | `terraform --help`                |
|   `terraform <subcommand> --help` | Show options for a specific subcommand                 | `terraform plan --help`           |
|                   `terraform fmt` | Reformat configuration files in current directory      | `terraform fmt`                   |
|        `terraform fmt -recursive` | Reformat files in current directory and subdirectories | `terraform fmt -recursive`        |

## Summary

* Enable autocomplete with `terraform -install-autocomplete` to speed up typing and reduce errors.
* Use `terraform --help` and `terraform <subcommand> --help` for quick, contextual CLI documentation.
* Keep Terraform code consistent with `terraform fmt`; add `-recursive` to format nested directories.

These built-in tools make it easy to discover commands, confirm flag behavior, and maintain consistent configuration style—all without leaving your terminal.

## Links and references

* Official Terraform documentation: [https://developer.hashicorp.com/terraform](https://developer.hashicorp.com/terraform)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/hashicorp-certified-terraform-associate-004/module/35cbc795-57ed-4af7-88c8-c9323af9294d/lesson/e6fcea39-8d6d-486e-8704-4853d693ad27" />
</CardGroup>
