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 Tools → Options → Environment → Keyboard (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 Edit → Advanced 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
Open command palette (Win: F1 or Ctrl+Shift+P)
Find "Change Language Mode"
Select language e.g.
json
. By now syntax should be highlighted.Format document (e.g. Open Command Palette -> "Format Document")
Unformat
- Select text
- Command Palette -> Join Lines
'Show the pics'
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
.
Related Topics
Windows.Forms.Panel 32767 Size Limit
Rotate a Graphics Bitmap at Its Center
How to Implement Batch Fetching with Fluent Nhibernate When Working with Oracle
Custom Xmlwriter to Skip a Certain Element
Overriding Fields or Properties in Subclasses
C# - How to Make Two Forms Reference Each Other
Why Are There System.*.* Dlls in My Bin Release Directory
Mvvm Show New Window from Vm When Seperated Projects
Wpf Webbrowser Mouse Events Not Working as Expected
ASP.NET Gridview: How to Edit and Delete Data Records
Unique Ways to Use the Null Coalescing Operator
Correct Implementation of a Custom Config Section with Nested Collections
How to Add Code Outside the Scope of Main When Using C# 9 Top Level Statements