Better autosave and autoformat in Visual Studio Code
This setup assumes that you have the ESLint and Prettier extensions installed in Visual Studio Code.
I like the autosave feature in editors: when you switch to another app — usually a browser — the file is automatically saved, causing the hot reload to apply the changes to the page right before you look at it.
One thing I’ve struggled with for a long time is that autoformatting (using Prettier) on autosave isn’t always desirable. For example, I start typing something, then google how to use a certain API, come back to the editor, and now everything is messed up by autoformatting.
I solved this by disabling autoformatting on save and running autoformat and save on a custom Cmd+S hotkey.
Two other useful things I like to do here:
Disable autosave when there’s a syntax error in the file, so autosave doesn’t break the dev server, which could cause loss of state, such as scroll position or form data.
Hide all autofixable ESLint issues so they don’t distract me while I’m writing code, since these issues don’t require any action from me and will be autofixed the next time I save the file.