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

Top comments (0)