How do I format all files in a Visual Studio Code project?

Solution 1:

Use the extension called ”Format Files”. Here are the steps:

  1. Download the extension called ”Format Files” on VSCode.
  2. Select and open the folder with files to format on VSCode.
  3. Press Ctrl+Shift+P to open command palette.
  4. Enter "Start Format Files: Workspace" and select this option.

Source: https://marketplace.visualstudio.com/items?itemName=jbockle.jbockle-format-files

Solution 2:

This works for me

Install prettier:

npm init 
npm i prettier

Add following script in package.json:

"pretty": "prettier --write \"./**/*.{js,jsx,json}\"" 

In this case only, i need to format my .js .jsx and .json files.

Run script:

npm run pretty

Solution 3:

The simplest solution that I have found is as below.

  • Install prettier in vscode.
  • Create the .prettierrc file and configure it the way you want.
  • Run following command in vscode console.

npx prettier --write "**/*.ts" (Add the file type regex as per the need)