Prettier Auto Formatting in Visual Studio Code.

Kiran Jasvanee
3 min readMar 22, 2020

--

Formatting a code is a pain, especially when working on a team. The modern-day development is that tooling has gotten so much better than before. While working on Node.js projects, I want the formatting of code to be done automatically, like while saving. Yes, we can do that by installing and setting up Prettier in Visual Studio code.

NOTE: You have to change preferences of Auto Formatting for your every new project you creates.

Sample Code — before formatting:

Adding Prettier using Extension:

  • Just go to Extensions, last one in the Activity Bar. If not familiar with Visual Studio layout, Go through — Visual Studio Layout Guide
  • Search Prettier, select Prettier — Code Formatter, and install it. The below image shows the installed layout.

Without Auto Formatting:

Just you are not a fan of Auto formatting, then skip to With Auto Formatting.

It’s really handy with Command Palette. Command+Shift+P to open it. It will show search bar with right arrow >, just start typing Format Document With, and you will come up with results as below.

  • Select Format Document With and again you will be asked for few options. Select Prettier — Code Formatter and your file will be formatted.

With Auto Formatting:

  • Usually, I choose to go to settings using Command Palette, Command+Shift+P to open it. It will show search bar with right arrow >, just start typing Settings and it will come with few options.
  • Select — Preferences: Open Workspace Settings, it will come up with a new tab called Settings.
  • In the Workspace section, select Text Editor and select Formatting. Check Format on Save, the second option in Formatting and we are done here.
  • Close the Settings tab and get back to the file. Just save now Command+S and file will be Saved and Formatted too.

I hope you got helped with the information provided. Happy Coding…

Thoughts? Feedback? Disagreements? Twitter me: @kiranjasvanee, Skype me: kiranjasvanee

If you enjoyed reading this post, please share and clap so others can find it 👏👏👏 !!!!

You can follow me on Medium for fresh articles.

--

--

Kiran Jasvanee
Kiran Jasvanee

Written by Kiran Jasvanee

Apple (iOS, watchOS, macOS & tvOS) developer. Creative engineering and data processing boosts me.

No responses yet