envcheck fix

Automatically fix formatting issues in .env files.

Usage

envcheck fix [OPTIONS] [FILES]...

Arguments

  • <FILES>... - One or more .env files to fix

Options

OptionDescription
--dry-runShow changes without modifying files
--commitAuto-commit fixes to current branch
--prCreate a PR with fixes (requires gh CLI)
--no-sortSkip key sorting
--no-trimSkip whitespace trimming
-h, --helpPrint help

What Gets Fixed

IssueFix
Trailing whitespaceTrim whitespace from end of lines
Unsorted keysSort keys alphabetically
Extra blank linesNormalize to single blank line between sections
Spacing around =Ensure KEY=value format (no spaces around =)

Examples

Fix single file

envcheck fix .env

Output:

Fixed .env:
  - Sorted 15 keys
  - Trimmed 3 trailing whitespaces

Dry run

envcheck fix .env --dry-run

Fix multiple files

envcheck fix .env .env.local .env.prod

Auto-commit fixes

envcheck fix .env --commit

Creates a commit:

[envcheck] Auto-fix .env

- Sorted keys alphabetically
- Trimmed trailing whitespace

Create PR

envcheck fix .env --pr

Creates a branch and PR:

Branch: envcheck/auto-fix
PR: "envcheck: Auto-fix .env formatting"

Safety

The fix command:

  • ✅ Preserves comments and empty lines
  • ✅ Preserves key values (only fixes formatting)
  • ✅ Creates backups before modifying
  • ⚠️ Does NOT fix duplicate keys (requires manual resolution)
  • ⚠️ Does NOT fix empty values (may be intentional)

Backups

Before fixing, a backup is created:

.env          →  .env.backup
.env.local    →  .env.local.backup

Remove backups after confirming fixes:

rm .env.backup

Git Integration

Pre-commit auto-fix

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/envcheck/envcheck
    rev: v0.1.0
    hooks:
      - id: envcheck-fix
        args: ["--commit"]

CI/CD with PR

- name: Fix and create PR
  run: envcheck fix .env --pr
  if: failure()

Configuration

Create .envcheckrc.yaml for project-specific settings:

fix:
  sort_keys: true
  trim_whitespace: true
  normalize_spacing: true
  create_backup: true

See Also