How to Automatically Format and Align Code in VS Code with Prettier
Learn how to use Prettier in VS Code to automatically align and format your code on save. Perfect for JavaScript, HTML, CSS, and more.
When coding in Visual Studio Code you may find yourself trying to format your code manually by using a combination of keyboard keys like Command + Shift + C, enter, and delete.
Thankfully there is a way to automatically format your code with the Prettier extension. With Prettier you can either format with a shortcut (Shift Option F on Mac) or set up automatic format on every file save.
Steps to Install Prettier
Within your Visual Studio code project go to extensions in the left sidebar (the icon with building blocks).
In the search bar, type Prettier
Select Prettier - Code Formatter by Prettier and click the blue Install button
How to Auto Format with keyboard shortcut from Prettier
Option 1: After making an update to your code use the Keybinding Shift Option F on Mac to fomat your code.
Option 2: Hold Command Shift P to run a command in the Visual Studio Code search bar, type Format Document then hit Enter
How to Auto Format on every save with Prettier
Type Command Shift P to run a command in the Visual Studio Code search bar or click the search bar in the top bar and enter the caret > symbol
After the caret symbol type Preferences:Open Workspace Settings (JSON)
Now on every save of your file you will see Prettier go into affect. You’ll also know that Prettier is correctly installed if you see it in the bottom right corner of the status bar.
No fluff. Just real projects, real value, and the path from code to cash — one useful build at a time.