Automatically fix formatting issues in .env files.
envcheck fix [OPTIONS] [FILES]...
<FILES>... - One or more .env files to fix| Option | Description |
|---|---|
--dry-run | Show changes without modifying files |
--commit | Auto-commit fixes to current branch |
--pr | Create a PR with fixes (requires gh CLI) |
--no-sort | Skip key sorting |
--no-trim | Skip whitespace trimming |
-h, --help | Print help |
| Issue | Fix |
|---|---|
| Trailing whitespace | Trim whitespace from end of lines |
| Unsorted keys | Sort keys alphabetically |
| Extra blank lines | Normalize to single blank line between sections |
Spacing around = | Ensure KEY=value format (no spaces around =) |
envcheck fix .env
Output:
Fixed .env:
- Sorted 15 keys
- Trimmed 3 trailing whitespaces
envcheck fix .env --dry-run
envcheck fix .env .env.local .env.prod
envcheck fix .env --commit
Creates a commit:
[envcheck] Auto-fix .env
- Sorted keys alphabetically
- Trimmed trailing whitespace
envcheck fix .env --pr
Creates a branch and PR:
Branch: envcheck/auto-fix
PR: "envcheck: Auto-fix .env formatting"
The fix command:
Before fixing, a backup is created:
.env → .env.backup
.env.local → .env.local.backup
Remove backups after confirming fixes:
rm .env.backup
# .pre-commit-config.yaml
repos:
- repo: https://github.com/envcheck/envcheck
rev: v0.1.0
hooks:
- id: envcheck-fix
args: ["--commit"]
- name: Fix and create PR
run: envcheck fix .env --pr
if: failure()
Create .envcheckrc.yaml for project-specific settings:
fix:
sort_keys: true
trim_whitespace: true
normalize_spacing: true
create_backup: true