Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 487
Brandon Clapp Avatar asked Apr 30 '15 16:04

Brandon Clapp


People also ask

Can you auto format in VS Code?

You can turn on the auto-formatting feature regardless of which programming language or code formatter you're using in VS Code. Most developers are using multiple code formatting extensions in VS Code, so you can ensure each of them automatically formats the file as you save it.

How do I format 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.


2 Answers

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.

like image 20
Bharathi Devarasu Avatar answered Sep 20 '22 08:09

Bharathi Devarasu


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 'Command Palette' 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 Model'

  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

like image 148
Brandon Clapp Avatar answered Sep 20 '22 08:09

Brandon Clapp