Env Sorter
Keep your .env files clean. Env Sorter clusters variables by prefix and
sorts each cluster alphabetically, on every save, as soon as it is installed
— no settings to change. It also registers as a document formatter, so it
runs on Format Document and participates in editor.formatOnSave if you use
that.
On top of sorting it also:
- Warns about duplicate keys — a squiggle on any variable defined twice in
the same file (most
.env loaders silently keep the last one).
- Diffs against a template — Env Sorter: Compare with .example reports which
keys your
.env is missing from (or has beyond) its .env.example sibling.
- Finds variables fast — Env Sorter: Find Variable is a fuzzy quick-pick
that jumps to a key, values previewed inline.
- Masks secrets — Env Sorter: Toggle Secret Value Masking hides
secret-looking values behind dots for screen-shares and screenshots, including
values quoted across multiple lines; the value under your cursor stays visible
so you can still edit.
Behaviour
- Grouped by prefix. The prefix is the segment before the first delimiter
(
DB_HOST, DB_NAME → the DB group). The delimiter is _ by default and
configurable (., -, …). Each group is separated from the next by a single
blank line. A variable with a unique prefix forms its own one-line block.
- Alphabetical. Groups are ordered by prefix; variables within a group are
ordered by key.
- Comments follow their variable. A run of comment lines directly above a
KEY=VALUE moves with it into its group.
- File header / footer are pinned. Comment lines before the first variable
stay at the top; comment lines after the last variable stay at the bottom.
- Multi-line quoted values are kept intact.
- Stable. Any blank lines the file already had are discarded and
regenerated, so sorting an already-sorted file changes nothing.
Example
# Local dev config
NODE_ENV=development
APP_PORT=3000
APP_NAME=demo
DB_PASSWORD=p@ss
DB_HOST=localhost
STRIPE_SECRET_KEY=sk_test
STRIPE_PUBLIC_KEY=pk_test
DEBUG=true
becomes
# Local dev config
APP_NAME=demo
APP_PORT=3000
DB_HOST=localhost
DB_PASSWORD=p@ss
DEBUG=true
NODE_ENV=development
STRIPE_PUBLIC_KEY=pk_test
STRIPE_SECRET_KEY=sk_test
Settings
| Setting |
Default |
Description |
envSorter.sortOnSave |
true |
Sort automatically on every save. Turn off to sort only manually via Format Document. |
envSorter.groupByPrefix |
true |
Cluster variables by prefix into blank-line-separated blocks. When false, produces one flat alphabetical list with a blank line wherever the first letter changes. |
envSorter.prefixDelimiter |
"_" |
The delimiter whose first occurrence ends the grouping prefix. Use "." or "-" for keys like db.host; leave empty to group each variable under its whole key. |
envSorter.caseSensitive |
false |
Sort case-sensitively (uppercase before lowercase). When off, FOO and foo sort together. |
envSorter.checkDuplicates |
true |
Warn on any key defined more than once in a file. |
envSorter.maskSecrets |
false |
Mask secret-looking values as soon as an env file opens (otherwise toggle per file with the command). |
envSorter.secretKeyPattern |
"" |
Case-insensitive regex matched against key names to decide which values are secret. Empty uses the built-in list (SECRET, TOKEN, PASSWORD, KEY, API, AUTH, …). Generated-looking values are masked regardless. |
Commands
Run these from the Command Palette while an env file is focused:
| Command |
What it does |
| Env Sorter: Compare with .example |
Reports keys missing from / extra in the current file versus its .env.example (or .sample / .template / .dist) sibling. |
| Env Sorter: Find Variable |
Fuzzy quick-pick over the file's keys; select one to jump to its line. |
| Env Sorter: Toggle Secret Value Masking |
Hides / shows secret-looking values for the current file. |
- A comment stays attached to a variable only when it sits directly above it
with no blank line between. That comment travels with the variable.
- A comment that is the first non-blank content in the file is treated as a file
header and stays at the top — it is never pulled down next to a variable.
- If a section-header comment (
# Database) sits directly above a variable that
is not the alphabetically-first one in its group, it will end up in the
middle of the group. Put section headers above the first member, or accept
that grouping already keeps the block together without the header.
Sorting on save
This is on by default — installing the extension is all it takes. It works
regardless of your editor.formatOnSave / editor.formatOnSaveMode /
editor.defaultFormatter settings, because the extension applies the sort itself
before the file is written. Set envSorter.sortOnSave to false to opt out.
If you would rather drive it through VS Code's own format-on-save instead, set
envSorter.sortOnSave to false and configure:
{
"editor.formatOnSave": true,
"[dotenv]": {
"editor.defaultFormatter": "abdur-rehman.env-sorter"
}
}
The dotenv language is contributed by this extension and matches .env,
.env.*, and *.env files.
Developing
npm install
npm run compile # or: npm run watch
npm test # runs the unit tests (sorter + env parser)
Press F5 in VS Code to launch an Extension Development Host.