How to format file in Visual Studio Code (hotkeys)?

Viewed 11222

How to format file | project folder | all project files (using hot keys AND using menu) in Visual Studio Code?

4 Answers

In case you are not able to format your code with any of these commands it's maybe because you did not set up your formatter configuration

I recommend you to use Prettier (install from Extensions)

  1. open the command palette (CTRL + SHIFT + P - on windows)
  2. search for prettier: Create Configuration File
  3. hit Enter and choose your project directory folder path
  4. if you can see a .prettierc file added into your directory, you must be able to use SHIFT + ALT + F to format your code

+1. also you are able to set your environment to automatically format your file at save.
You can do it writing this line in settings.json:

"editor.formatOnSave": true

Hint: Access it using the command palette again and writing settings.json, and select
Preference: Open Settings (JSON)

Related