DEV Community

Corey Alexander
Corey Alexander Subscriber

Posted on • Originally published at coreyja.com on

Autoformat in VSCode on Auto-Saves

I've had VS Code setup to format my code on save for a while now, and I love it!

Recently though I had a few files slip through my auto-formatting and be caught in CI. When I investigated it turns out that my VS Code settings were prevening auto-formatting from running on auto-save.

I had files.autoSave set to afterDelay and files.autoSaveDelay set to 300. Which meant that after 300ms my code would auto-save. However, auto-formatting would only run on manual saves, not auto-saves triggered by afterDelay.

The fix was pretty simple! I switched from afterDelay to onFocusChange! Now my files get changed anytime I switch to a different window or file, and even better auto-formatting runs as well!

Here is a snippet from my new config with a comment I left for future me:

"files.autoSave": "onFocusChange",
// By setting files.autoSave to `onFocusChange` the delay below isn't needed
// We need to use `onFocusChange` (or `onWindowChange`) since auto formatting isn't
// applied when using `afterDelay` :sadnerd:
// "files.autoSaveDelay": 300,
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay