在Windows的Visual Studio中,有没有类似于 Ctrl + K + F 和 Ctrl + K + D这样的快捷键用于格式化或“美化”代码?同样的问题也适用于Visual Studio Code编辑器。
在Windows的Visual Studio中,有没有类似于 Ctrl + K + F 和 Ctrl + K + D这样的快捷键用于格式化或“美化”代码?同样的问题也适用于Visual Studio Code编辑器。
在Visual Studio Code中,代码格式化可以通过以下快捷键实现:
另外,您也可以通过子菜单View / Command Palette
(Win: Ctrl + Shift + P) 或 (Mac: Command + Shift + P) 找到这些快捷键及其他快捷键,并搜索format document进行格式化。
打开命令面板 (Win: F1 或 Ctrl+Shift+P)
找到“更改语言模式”
选择要使用的语言,例如json
。此时语法应该会被突出显示。
格式化文档(例如:打开命令面板->“Format Document”)
代码格式化快捷键:
Windows下的Visual Studio Code - Shift + Alt + F
MacOS下的Visual Studio Code - Shift + Option + F
Ubuntu下的Visual Studio Code - Ctrl + Shift + I
如果需要,可以使用首选项设置自定义此快捷方式。
保存文件时进行代码格式化:
Visual Studio Code 允许用户自定义默认设置。
如果希望在保存时自动格式化内容,请将以下代码片段添加到 Visual Studio Code 的工作区设置中。
菜单 文件 → 首选项 → 工作区设置
{
// 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"
]
}
注意:现在您可以自动格式化TypeScript文件。请查看我的更新。
Shift + Alt + I
。 - a11smilesprettier.singleQuote: true
。 - Enn文件 --> 首选项 --> 设置
,然后在搜索栏下面你会看到 用户
,右边写着 工作区
。 - Mark van der Dam您可以在菜单文件 → 首选项 → 键盘快捷键中添加一个按键绑定。
{ "key": "cmd+k cmd+d", "command": "editor.action.formatDocument" }
或者类似于 Visual Studio:
{ "key": "ctrl+k ctrl+d", "command": "editor.action.formatDocument" }
Ctrl
+ Shift
+ I
。 - Hamza KhanzadaVisual Studio Code 1.6.1支持“保存时格式化”,它将自动获取相关安装的格式化扩展,并在每次保存时对整个文档进行格式化。
通过设置启用“保存时格式化”。
"editor.formatOnSave": true
还有可用的快捷键(Visual Studio Code 1.7及以上版本):
格式化整个文档:Shift + Alt + F
仅格式化所选内容:Ctrl + K,Ctrl + F
针对 Fedora 的设置:
文件
-> 首选项
-> 键盘快捷键
.默认键盘快捷键
中,使用 (Ctrl + F) 搜索 editor.action.format
。我的设置是 "key": "ctrl+shift+i"
您也可以更改它。请参考这个答案来了解如何更改...或者如果您感到有些懒得向上滚动:
您可以在“首选项->键盘快捷键”中添加一个按键绑定
{ "key": "cmd+k cmd+d", "command": "editor.action.format" }
或类似于 Visual Studio:
{ "key": "ctrl+k ctrl+d", "command": "editor.action.format" }
请注意:cmd 键仅用于 Mac。对于 Windows 和 Fedora(Windows 键盘),请使用 Ctrl
编辑:
根据 Visual Code 版本 1.28.2
,我发现以下内容。
editor.action.format
不再存在。它现在被 editor.action.formatDocument
和 editor.action.formatSelection
取代。
在搜索框中键入 editor.action.format
以查看现有的快捷键。
按照以下步骤更改键组合:
editor.action.formatDocument
或 editor.action.formatSelection
CTRL + SHIFT + I
在 JavaScript 中可行,但在 PHP 中不可行。 - Jamie Carl菜单 文件 → 偏好设置 → 设置
"editor.formatOnType": true
输入分号时,它将被格式化。
另外,您也可以使用"editor.formatOnSave": true
。
shift+alt+f
不同。不知道为什么! - default_noob_network