可通过以下快捷方式在Visual Studio代码中使用代码格式:
或者,您可以通过编辑器中提供的“命令面板”(在Mac上使用
>
查找“更改语言模型” 选择语言,例如 格式化文档(例如打开命令调色板-&>“格式化文档”)
代码格式设置快捷方式:
Windows上的Visual Studio代码-
MacOS上的Visual Studio代码-
Ubuntu上的Visual Studio代码-
如果需要,还可以使用首选项设置自定义此快捷方式。
保存文件时的代码格式:
Visual Studio代码允许用户自定义默认设置。
如果要在保存时自动设置内容的格式,请在Visual Studio代码的工作区设置中添加以下代码段。
菜单文件首选项工作区设置
{
// 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文件。查看我的更新。
您可以在菜单,文件,首选项,键盘快捷键中添加键绑定。
{ "key": "cmd+k cmd+d", "command": "editor.action.formatDocument" }
或Visual Studio,如:
{ "key": "ctrl+k ctrl+d", "command": "editor.action.formatDocument" }