Is There a Format Code Shortcut for Visual Studio

Is there a format code shortcut for Visual Studio?

Visual Studio with C# key bindings

To answer the specific question, in C# you are likely to be using the C# keyboard mapping scheme, which will use these hotkeys by default:

Ctrl+E, Ctrl+D to format the entire document.

Ctrl+E, Ctrl+F to format the selection.

You can change these in menu ToolsOptionsEnvironmentKeyboard (either by selecting a different "keyboard mapping scheme", or binding individual keys to the commands "Edit.FormatDocument" and "Edit.FormatSelection").

If you have not chosen to use the C# keyboard mapping scheme, then you may find the key shortcuts are different. For example, if you are not using the C# bindings, the keys are likely to be:

Ctrl + K + D (Entire document)

Ctrl + K + F (Selection only)

To find out which key bindings apply in your copy of Visual Studio, look in menu EditAdvanced menu - the keys are displayed to the right of the menu items, so it's easy to discover what they are on your system.


(Please do not edit this answer to change the key bindings above to what your system has!)

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

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'

Sample Image
Sample Image

How do you auto format code in Visual Studio?

To format a selection: Ctrl+K, Ctrl+F

To format a document: Ctrl+K, Ctrl+D

See the pre-defined keyboard shortcuts. (These two are Edit.FormatSelection and Edit.FormatDocument.)

Note for macOS

On macOS, use the CMD ⌘ key instead of Ctrl:

  • To format a selection: CMD ⌘+K, CMD ⌘+F
  • To format a document:
    CMD ⌘+K, CMD ⌘+D

How to format code in Visual Studio Code on Windows

If you are working on PHP you should download the PHP extension format code.

Press F1 and type:

ext install PHP Code Format

Then Shift + Alt + F will work.

Automatically format code in Visual Studio Code

You can choose one of the below options

"editor.formatOnType": false,
"editor.formatOnPaste": false,
"editor.formatOnSave": false

Or create a custom keyboard shortcut by editing editor.action.formatDocument. But I AFAIK there is no option to execute the command whenever you press ;. You can set a keyboard shortcut, but I guess you're not able to write ; anymore then :D

How do you auto format code in Visual Studio on save?

Since Visual Studio 2022 17.1 there is a builtin Feature to run code formatting on save (see devblogs.microsoft), meaning there is no need to install extensions like Format document on Save.

Sample Image



Related Topics



Leave a reply



Submit