Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I format all files in Visual Studio Code?

VS Code has great support for source code formatting. The editor has two explicit format actions: Format Document (Ctrl+Shift+I) - Format the entire active file. Format Selection (Ctrl+K Ctrl+F) - Format the selected text.

How do I format an entire document in Visual Studio?

As long as your code is syntactically correct (i.e. no stray brackets or End Ifs without matching Ifs), Visual Studio will reformat your whole file with one key chord: Hold down the Control key and then press K, followed by D. Boom!

What is the fastest way to format code in Visual Studio?

In the default configuration for Visual Studio Code, the command can be run using the shortcut Alt+Shift+F. To format a range, in an already opened project, open the document that you want to modify, select the specific range to format, right-click, and select Format Selection.

How do I Auto beautify code in Visual Studio?

Auto formatting settings in Visual Studio Show activity on this post. Select the text you want to automatically indent. Click menu Edit → Advanced → *Format Selection, or press Ctrl + K , Ctrl + F . Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.


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


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

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)


I was out of luck finding an extension that was doing this the way I was expecting it so I made one. I suggest you take a look at the extension I just made :

https://marketplace.visualstudio.com/items?itemName=lacroixdavid1.vscode-format-context-menu#overview

It might still have some issues, feel free to report them or to contribute.


With npm, just run npx prettier --write . in the terminal if your language is supported


You can use the extension Command on All Files

Add this to your settings.json

"commandOnAllFiles.commands": {
    "Format File": {
      "command": "editor.action.formatDocument",
      "includeFileExtensions": [".js",".ts",".css"]
    }
  }

From the command palette select command Apply 1 or more commands to all files in the Workspace and select Format File from the Quickpick list. Wait till you see an information message (lower right) that all files have been processed.


I do a simply trick:

  1. download this extension https://marketplace.visualstudio.com/items?itemName=ApceHHypocrite.OpenAllFiles
  2. open all files
  3. set "editor.formatOnSave": true
  4. save all files

Hope it helps