Tfsort
A CLI utility to sort Terraform variables and outputs
Install / Use
/learn @AlexNabokikh/TfsortREADME
tfsort

tfsort is a command-line utility designed for meticulous engineers who prefer to keep their Terraform variable, output, locals and terraform blocks sorted alphabetically. It also corrects spacing issues between these blocks and removes leading or trailing newlines in the processed files. tfsort can read from files or stdin, write to files or stdout, and process directories recursively.
Contents
- Demo
- Key Features
- Supported File Types
- Installation
- Usage
- Examples
- Contributing
- Code of Conduct
- Author
- License
Demo
Key Features
- Alphabetical Sorting: Sorts
variable,output,localsandterraformblocks within your HCL files. - Flexible Input/Output:
- Read from a specific file, directory or standard input (stdin).
- Overwrite the input file, write to a new file, or print to standard output (stdout).
- Recursive Processing: Sort files in an entire directory and its subdirectories.
- Skips common version control (
.git) and Terraform utility directories (.terraform,.terragrunt-cache).
- Skips common version control (
- Dry Run Mode: Preview changes without modifying any files.
- Code Formatting:
- Corrects spacing between sorted blocks.
- Removes unnecessary leading or trailing newlines from the file.
Supported File Types
tfsort processes files with the following extensions:
.tf.hcl.tofu
Installation
Homebrew
To install tfsort using Homebrew:
-
Add the tap:
brew tap alexnabokikh/tfsort -
Install
tfsort:brew install tfsort
Nix
nix-env -iA nixpkgs.tfsort
Chocolatey (Windows)
To install tfsort using Chocolatey:
choco install tfsort
Using go install
If you have Go installed and configured, you can install tfsort directly using go install:
go install github.com/AlexNabokikh/tfsort@latest
This command will download the source code, compile it, and install the tfsort binary into your $GOPATH/bin or $GOBIN directory (make sure this directory is in your system's PATH).
Binary Release
Download the latest binary release for your operating system from the Releases Page.
From Source
Alternatively, build tfsort from source:
-
Clone the repository:
git clone https://github.com/AlexNabokikh/tfsort.git cd tfsort -
Build the binary:
go build .This will create a
tfsortexecutable in the current directory.
Usage
Command Synopsis
tfsort [flags] [files...]
Arguments
files- Path to Terraform/HCL files (e.g.,
variables.tf) - Path to directories to process recursively
- The character
-instructstfsortto read input from _standard input (stdin). For examplecat file.tf | tfsort -will read from stdin.
- Path to Terraform/HCL files (e.g.,
- If no arguments are provided and stdin is not a pipe,
tfsortwill show the help message.
Flags
-o, --out <path>:- Specifies the path to the output file.
- If the input is a file and
-ois not provided, the input file is overwritten. - If the input is stdin and
-ois not provided, the output is sent to stdout. - This flag cannot be used with
-r, --recursive.
-d, --dry-run:- Previews the changes by printing the sorted content to stdout.
- No files will be modified when this flag is used.
-h, --help:- Displays a comprehensive help message, listing available commands, arguments, and flags with their descriptions.
-v, --version:- Displays the installed version of the
tfsortapplication, typically including the version number, commit hash, and build date if available.
- Displays the installed version of the
Examples
-
Sort a single file in-place: (Sorts
variable,output,localsandterraformblocks inmy_variables.tfand overwrites the file)tfsort my_variables.tf -
Sort a single file and write to a new file:
tfsort -o sorted_variables.tf my_variables.tf -
Preview changes for a single file (dry run): (Prints the sorted content to the console without modifying
my_variables.tf)tfsort -d my_variables.tf -
Sort content from stdin and print to stdout:
cat my_config.tf | tfsort - -
Sort content from stdin and write to a file:
cat my_config.tf | tfsort -o sorted_from_stdin.tf - -
Recursively sort files in a directory (in-place): (Sorts all
.tf,.hcl,.tofufiles inmy_terraform_project/and its subdirectories, modifying them in-place. Skips.git,.terraform,.terragrunt-cache.)tfsort ./my_terraform_project/ -
Recursively sort files in a directory (dry run): (Prints what would be changed for each file to the console without modifying them.)
tfsort -d ./my_terraform_project/
Contributing
Contributions are welcome! Please read the CONTRIBUTING.md file for guidelines on how to contribute to this project, including code contributions, bug reports, and feature suggestions.
Code of Conduct
This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code. Please read the CODE_OF_CONDUCT.md file for details.
Author
This project was created by Alexander Nabokikh.
License
This software is available under the following licenses:
Related Skills
node-connect
346.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
346.4kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
107.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
