As of September 2016 (VSCode 1.6), this is now officially supported.
Add the following to your settings.json file:
"editor.formatOnSave": true
Here is the menu to access the settings

As of September 2016 (VSCode 1.6), this is now officially supported.
Add the following to your settings.json file:
"editor.formatOnSave": true
Here is the menu to access the settings

No need to add commands anymore. For those who are new to Visual Studio Code and searching for an easy way to format code on saving, kindly follow the below steps.
- Open Settings by pressing
[Cmd+,]in Mac (or[Ctrl+,]in Windows/Linux) or using the below screenshot.

- Type 'format' in the search box and enable the option 'Format On Save'.

You are done. Thank you.
Format on Save (prettier) stopped working with latest update
To The Developer Who Decided Format-On-Save On By Default was a Good Idea
Setting up Auto Format on Save easily in VS Code (Visual Studio Code) for Expo React-Native - Stack Overflow
Sorting lines alphabetically on save
This may help you:
https://marketplace.visualstudio.com/items?itemName=richie5um2.vscode-sort-json
Try searching for "Sort JSON" or similar, and perhaps you may find what you're looking for.
Videos
... you've helped me bring my code to the next level.
Thankyou everyone for your valuable advice, I remain in awe of all this community offers. I lost a bit of formatting I wanted to keep, but I'm sure I came out ahead in the end.
I'm more of the "edit the JSON" type of user of VSCode, and the answer from Masud was confusing to me because of that. It did push me in the right direction and I want to share for other like myself, more familiar with the .vscode/settings.json(s) than with that graphical Settings Editor.
settings.json edits - add these:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
.prettierrc edit or add a file by that name at the root of your project for any custom settings. Here are some of my favs:
{
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none"
}
Then, you can sanity check against those rules. I just added a trailing comma in an object block (in a Styles block in the sample Expo tsx component, in my case - brand new project) and see it removed on save... Then, remove the trailingComma rule, save, and try the comma again and see that the comma stays.
Regarding Auto Code Formatting:
For anyone starting out with VSCode - Expo, React-Native devs or having some issues with code formatting, please find below the simplest solution I could find (ESLint is a bit confusing / complex unless you are fairly familiar) - link to Expo guide is here if you would really like to do this (https://docs.expo.dev/guides/using-eslint/) - It is a bit complicated if you are just starting out and I find ESLint to cause too much bloat messaging on my code editor screen, especially now that there are things such as copilot etc.
[Instructions] Simply set up Prettier (no ESLint):
- Add 'Prettier - Code Formatter' extension in VSCode and enable it
- Go to the VSCode settings to set both the User and Workspace fields for a) Editor: Default Formatter to "Prettier - Code formatter", then b) check to enable Format on Save (I also checked Notebook > Format on Save).
Now get to Coding!!!
Some Next Steps:
You can check out the Expo Guide (or similar) for further best practices but it will take some time to get it all setup.
Prettier should already auto-format on save and you can further configure it by adding a .prettierrc file at the root of your project and following https://github.com/expo/expo/tree/main/packages/eslint-config-universe#customizing-prettier which should override the default settings you have set up for all your projects.
You can later setup linting as well which is generally more useful down the line for when working with remote/shared repositories (ie: they contain explanations for the correction, etc.). Causes a bit of bloat but it's quite helpful for navigating a new team / codebase (ex Amazon SDE) to understand the style guidelines being used, fixing an entire project programmatically via a script etc. with ESLint.