.env file sorter — alphabetise variables and find duplicate keys

Paste a .env file to get its variables sorted A to Z, with comments and multi-line quoted values kept with their keys and any duplicate key flagged, all without the file leaving your browser.

Live demo · real result from this tool

Sorted .env

API_KEY=replace-meAPP_NAME="ToolsRift"DB_HOST=localhostDB_HOST=db.internal# DatabaseDB_USER=app# AppPORT=3000PRIVATE_KEY="-----BEGIN KEY-----abc-----END KEY-----" 
Variables7

Duplicate keys: DB_HOST. Which value wins depends on the loader, so remove the extra one.

Processed on your device — nothing is uploaded.

How to use the env file sorter

  1. Paste the contents of your .env file into the .env box. The sample has comments, a duplicate DB_HOST and a multi-line private key.
  2. Keep Ignore case when sorting ticked for natural A–Z order.
  3. Tick Blank line between prefix groups (DB_, API_…) to separate variables by their first word.
  4. Check the Variables count and any duplicate warning, then copy the Sorted .env or download it.

What it does and when to use it

.env files grow by appending: someone adds DB_HOST at the bottom months after the first one was set at the top. Sorting brings related settings together and reveals keys defined twice. Use it before committing a .env.example or when a setting you changed seems to have no effect.

To compare two sorted files, use the code diff viewer. For other config formats, try the Java properties formatter or the INI formatter. To build a new file from JSON, see JSON to .env.

How it works

The sorter reads the file line by line in your browser:

  • A line like NAME=value or export NAME=value is a variable. Names may contain letters, digits, _, . and -.
  • Lines starting with #, and any line that is not a variable, are held and attached to the next variable.
  • If a value opens a quote that does not close on the same line, the following lines are added to that variable until the closing quote.
  • Blank lines are dropped, the variables are sorted by name, and each is written with its comments above it, exactly as typed.

Duplicates are found by exact name. With grouping on, a blank line is inserted whenever the part of the name before the first _ or . changes.

Worked examples

These are the tool’s own outputs.

The sample. Seven variables come out in this order: API_KEY, APP_NAME, DB_HOST (twice), DB_USER with # Database above it, PORT with # App above it, and the three-line PRIVATE_KEY. The note reads “Duplicate keys: DB_HOST. Which value wins depends on the loader, so remove the extra one.”

With prefix groups. API_KEY and APP_NAME land in separate groups because their first words differ (API and APP), while both DB_ variables share one.

Case-sensitive order. With Ignore case unticked, API_KEY, export NODE_ENV, api_url and b sort in that order: upper case first, and export does not count.

Limits and tips

  • Values are never changed, so quoting or escaping problems in your file remain.
  • Duplicates are flagged, not removed.
  • Variable expansion such as ${DB_HOST} is not evaluated, and sorting may move a variable above the one it refers to.

Frequently asked questions

Is it safe to paste real secrets here?
The sorting runs entirely in your browser and the text is not sent anywhere. For production secrets, you can still swap the values for placeholders before pasting.
Which value wins when a key appears twice?
It depends on the library or platform that reads the file, and you should not rely on any particular rule. That is why the tool warns instead of choosing for you. Delete the line you do not want.
Why did my section comments move?
Each comment stays with the variable directly below it. A heading such as App above a group of variables is attached only to the first variable, so after sorting it sits above that one key. Use Blank line between prefix groups to get sections back by prefix.
Are multi-line values safe?
Yes. A value that opens a double, single or back quote and closes it on a later line, such as a private key, is moved as one block, and its inner lines are not treated as variables.
What does Ignore case when sorting change?
With it ticked (the default), api_url sorts next to API_KEY. Unticked, keys sort by character code, so all upper-case names come before lower-case ones. The export prefix is ignored either way.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close