在Visual Studio中,Windows上的Ctrl + K + F和Ctrl + K + D用于格式化,或在Visual Studio代码编辑器中“美化”代码的等效功能是什么?
当前回答
Shift + Alt + F在1.17.2和更高版本中可以很好地完成这项工作。
其他回答
我在Visual Studio Code (Ubuntu)中使用的最简单的方法是:
用鼠标选择要设置格式的文本。
右击并选择“格式选择”。
代码格式化快捷方式:
Visual Studio Code on Windows - Shift + Alt + F
Visual Studio Code on MacOS - Shift + Option + F
Visual Studio Code on Ubuntu - 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文件。看看我的更新。
Visual Studio Code 1.6.1支持“保存时格式化”,它将自动拾取相关的已安装的格式化器扩展,并在每次保存时格式化整个文档。
通过设置启用“Format On Save”
"editor.formatOnSave": true
还有可用的键盘快捷键(Visual Studio Code 1.7及以上):
格式化整个文档:Shift + Alt + F
格式选择:Ctrl + K, Ctrl + F
默认情况下,这个键在HTML、CSS和JavaScript文档上不起作用。
经过搜索,我找到了流行的插件JS-CSS-HTML Formatter,安装量为133,796。
安装后,只需重新加载窗口并按Ctrl + Shift + F,成功了!
您可以在菜单文件→首选项→键盘快捷键中添加按键绑定。
{ "key": "cmd+k cmd+d", "command": "editor.action.formatDocument" }
或者像Visual Studio那样:
{ "key": "ctrl+k ctrl+d", "command": "editor.action.formatDocument" }
推荐文章
- 打开同一目录两次
- 如何从终端/命令行调用VS代码编辑器
- 如何在Visual Studio代码中重置设置?
- 如何将制表符转换为空格,反之亦然,在现有的文件
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- Visual Studio Code: .git文件夹/文件隐藏
- c++中不必要的花括号
- Visual Studio Code:如何调试Python脚本的参数
- 如何在Visual Studio代码中触发参数提示?
- 在notepad++中格式化代码
- 在VS Code中禁用“Comments are not allowed In JSON”错误
- 使用正则表达式搜索和替换Visual Studio代码
- 如何缩进/格式选择的代码在Visual Studio代码?
- 如何禁用预览文件与点击在vs代码?
- 在Python中格式化多行字典的正确方法是什么?