Brandon Clapp
Brandon Clapp

Reputation: 70553

How do you format code in Visual Studio Code (VSCode)?

What is the equivalent of Ctrl + K + F and Ctrl + K + D on Windows in Visual Studio for formatting, or "beautifying" code in the Visual Studio Code editor?

Upvotes: 988

Views: 4683447

Answers (29)

Bharathi Devarasu
Bharathi Devarasu

Reputation: 8327

Code Formatting Shortcut:

Visual Studio Code on Windows - Shift + Alt + F

Visual Studio Code on MacOS - Shift + Option + F

Visual Studio Code on Ubuntu - Ctrl + Shift + I

You can also customize this shortcut using a preference setting if needed.

Code Formatting While Saving the File:

Visual Studio Code allows the user to customize the default settings.

If you want to auto format your content while saving, add the below code snippet in the work space settings of Visual Studio Code.

Menu FilePreferencesWorkspace Settings

{
  // Controls if the editor should automatically format the line after typing
  "beautify.onSave": true,

  "editor.formatOnSave": true,

  // You can auto format any files based on the file extensions type.
  "beautify.JSfiles": [
      "js",
      "json",
      "jsbeautifyrc",
      "jshintrc",
      "ts"
  ]
}

Note: now you can auto format TypeScript files. Check my update.

Upvotes: 591

Brandon Clapp
Brandon Clapp

Reputation: 70553

The code formatting is available in Visual Studio Code through the following shortcuts:

  • On Windows Shift + Alt + F
  • On Mac Shift + Option + F
  • On Linux Ctrl + Shift + I

Alternatively, you can find the shortcut, as well as other shortcuts, through the submenu View / Command Palette, also provided in the editor with Ctrl +Shift+ P (or Command + Shift + P on Mac), and then searching for format document.

For unsaved snippets

  1. Open command palette (Win: F1 or Ctrl+Shift+P)

  2. Find "Change Language Mode"

  3. Select language e.g. json. By now syntax should be highlighted.

  4. Format document (e.g. Open Command Palette -> "Format Document")

Unformat

  1. Select text
  2. Command Palette -> Join Lines

'Show the pics'

enter image description here enter image description here

Upvotes: 5895

Siddarth Kanted
Siddarth Kanted

Reputation: 5914

  1. Right click somewhere in the content area (text) for the file
  2. Select Format Document from the menu:
    • Windows: Alt+Shift+F
    • Linux: Alt+Shift+I
    • macOS: ++F

Enter image description here

Upvotes: 247

Ashutosh Jha
Ashutosh Jha

Reputation: 16367

The simplest way I use in Visual Studio Code (Ubuntu) is:

Select the text which you want to format with the mouse.

Right click and choose "Format selection".

Upvotes: 23

Rudy
Rudy

Reputation: 464

Just install Visual Studio Keymap (Visual Studio Keymap for Visual Studio Code) by Microsoft. Problem Solved. :p

Upvotes: 9

mumair
mumair

Reputation: 2818

By default, this key was not working for me on HTML, CSS, and JavaScript documents.

After searching, I found the popular plugin JS-CSS-HTML Formatter with 133,796 installs.

After installation, just reload the windows and hit Ctrl + Shift + F, and it works!

Upvotes: 10

M Fatih Koca
M Fatih Koca

Reputation: 601

Menu FilePreferencesSettings

"editor.formatOnType": true

When you enter the semicolon, it's going to be formatted.

Alternatively, you can also use "editor.formatOnSave": true.

Upvotes: 45

Rick
Rick

Reputation: 13500

For those that want to customize what JavaScript files to format, you can use the any extension on the JSfiles property. The same applies to HTML.

{
    "beautify.onSave": true,
    "beautify.JSfiles": ["js", "json", "jsbeautifyrc", "jshintrc", "ts"],
    "beautify.HTMLfiles": ["htm", "html"]
}

This will enable beautify on save for TypeScript, and you can add in XML to the HTML option.

Upvotes: 8

afeef
afeef

Reputation: 4716

Formatting code in Visual Studio.

I have tried to format in Windows 8.

Just follow the screenshots below.

  1. Click on View on the top menu bar, and then click on Command Palette.

    Enter image description here

  2. Then a textbox would appear where we need type Format

    Shift + Alt + F

    Enter image description here

Upvotes: 27

Akhil Ghatiki
Akhil Ghatiki

Reputation: 1208

Select the text, right click on the selection, and select the option "command palette":

Enter image description here

A new window opens. Search for "format" and select the option which has formatting as per the requirement.

Upvotes: 10

Lukasz Czerwinski
Lukasz Czerwinski

Reputation: 15482

On Mac, Shift + Alt + F works for me.

You can always check the key bindings in the menu:

Menu FilePreferencesKeyboard Shortcuts and filter by keyword 'format'.

Upvotes: 21

Piotrek Hryciuk
Piotrek Hryciuk

Reputation: 805

Shift + Alt + F does the job just fine in 1.17.2 and above.

Upvotes: 28

Aleksander Tymczenko
Aleksander Tymczenko

Reputation: 269

For some reason Alt + Shift + F didn't work for me on Mac Visual Studio Code 1.3.1, and actually the command "Format Document" don't worked at all. But command Formatter worked very well.

So you can use Command + Shift + P and type Formatter or make your own shortcut in menu FilePreferencesKeyboard ShortcutsCommand + K Command + S then type Formatter and add your shortcut.

See an example:

Enter image description here

Upvotes: 26

Dariusz Tarczynski
Dariusz Tarczynski

Reputation: 16731

Visual Studio Code 1.6.1 supports "Format On Save" which will automatically pick up relevant installed formatter extensions and format the whole document on each save.

Enable "Format On Save" by setting

"editor.formatOnSave": true

And there are available keyboard shortcuts (Visual Studio Code 1.7 and above):

Format the whole document: Shift + Alt + F

Format Selection only: Ctrl + K, Ctrl + F

Upvotes: 84

ESP32
ESP32

Reputation: 8713

Just right-click on the text and select "Format code".

Visual Studio Code uses js-beautify internally, but it lacks the ability to modify the style you wish to use. The extension "beautify" lets you add settings.

Upvotes: 37

Hvitis
Hvitis

Reputation: 555

Visual Studio Code on Linux:

Ctrl + [ to unindent block of code and

Ctrl + ] to make a mass indentation

Upvotes: 7

Nigel Feasey
Nigel Feasey

Reputation: 79

You have to install the appropriate plug-in first (i.e., XML, C#, etc.).

Formatting won't become available until you've installed the relevant plugin, and saved the file with an appropriate extension.

Upvotes: 8

Roi Shabtai
Roi Shabtai

Reputation: 3085

While changing the default behavior for Visual Studio Code requires an extension, you may override the default behavior in the workspace or user level. It works for most of the supported languages (I can guarantee HTML, JavaScript, and C#).

Workspace level

Benefits

  • Does not require an extension
  • Can be shared among teams

Outcomes

  • .vscode/settings.json is created in the project root folder

How To?

  1. Go to: Menu FilePreferencesWorkspace Settings

  2. Add and save "editor.formatOnType": true to settings.json (which overrides default behavior for the project you work on by creating .vscode/settings.json file).

    How it looks

User environment level

Benefits

  • Does not requires extension
  • Personal development environment tweeking to rule them all (settings:))

Outcomes

  • User's settings.json is modified (see location by operating system below)

How To?

  1. Go to: menu FilePreferencesUser Settings

  2. Add or change the value of "editor.formatOnType": false to "editor.formatOnType": true in the user settings.json

Your Visual Studio Code user's settings.json location is:

Settings file locations depending on your platform, the user settings file is located here:

  • Windows: %APPDATA%\Code\User\settings.json
  • Mac: $HOME/Library/Application Support/Code/User/settings.json
  • Linux: $HOME/.config/Code/User/settings.json The workspace setting file is located under the .vscode folder in your project.

More details may be found here.

Upvotes: 16

Gyuha Shin
Gyuha Shin

Reputation: 151

Use extension...

Enables auto formatting of the code when you save a file.

Launch Visual Studio Code and Quick Open (Ctrl + P), paste the following command, and press Enter.

ext install format-on-save

https://marketplace.visualstudio.com/items?itemName=gyuha.format-on-save

Upvotes: 1

杨恩锋
杨恩锋

Reputation: 79

Not this one. Use this:

Menu FilePreferencesWorkspace Settings, "editor.formatOnType": true

Upvotes: 6

Burk
Burk

Reputation: 3067

In Visual Studio Code, Shift+Alt+F is doing what Ctrl+K+D is doing in Visual Studio.

Upvotes: 25

ranbuch
ranbuch

Reputation: 1642

On Ubuntu it's Ctrl + Shift + I.

Upvotes: 48

Jo Smo
Jo Smo

Reputation: 7179

The format shortcut in C# didn't work for me until I installed Mono for Mac OS X, DNVM and DNX.

Before I installed Mono, the auto-format shortcut (Shift + Alt + F) worked only for the .json files.

Upvotes: 20

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26652

On Linux it is Ctrl + Shift + I.

On Windows it is Alt + Shift + F. Tested with HTML/CSS/JavaScript and Visual Studio Code 1.18.0.

For other languages, you might need to install a specific language package.

Upvotes: 71

César
César

Reputation: 1371

The right key combination is Shift + Alt + F.

Upvotes: 127

hardkoded
hardkoded

Reputation: 21695

You can add a keybinding in menu FilePreferencesKeyboard shortcuts.

{ "key": "cmd+k cmd+d", "command": "editor.action.formatDocument" }

Or Visual Studio like:

{ "key": "ctrl+k ctrl+d", "command": "editor.action.formatDocument" }

Upvotes: 236

abyshukla
abyshukla

Reputation: 791

For Fedora

  1. Click File -> Preferences -> Keyboard shortcuts.
  2. Under Default Keyboard Shortcuts, search (Ctrl + F) for editor.action.format.

Mine read "key": "ctrl+shift+i"

You can change it as well. Refer to this answer on how to... or if you are feeling a little lazy to scroll up:


You can add a keybinding in "Preferences->Keyboard shortcuts"

{ "key": "cmd+k cmd+d", "command": "editor.action.format" }

Or Visual Studio like:

{ "key": "ctrl+k ctrl+d", "command": "editor.action.format" }


Please note: cmd key is only for Macs. For Windows and Fedora (Windows keyboard) use Ctrl


EDIT:

As per Visual Code version 1.28.2 this is what I found.

editor.action.format no longer exists. It has now been replaced by editor.action.formatDocument and editor.action.formatSelection.

Type editor.action.format in the search box to view existing shortcuts.

To change the key combinations follow these steps:

  1. Click editor.action.formatDocument or editor.action.formatSelection
  2. A pen like icon appears to the left - click it.
  3. A pop-up appears. Press the desired key combination and press enter.

Upvotes: 67

Gopal Devra
Gopal Devra

Reputation: 787

In Mac, use +K and then +F.

Upvotes: 7

Jerry Ni
Jerry Ni

Reputation: 531

If you want to custom the style of format-document, you should use Beautify extention.

Refer to this screenshot:

img

Upvotes: 6

Related Questions