在Visual Studio中,Windows上的Ctrl + K + F和Ctrl + K + D用于格式化,或在Visual Studio代码编辑器中“美化”代码的等效功能是什么?


当前回答

代码格式化快捷方式:

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文件。看看我的更新。

其他回答

右键组合为Shift + Alt + F。

对于那些想要自定义要格式化JavaScript文件的人,可以使用JSfiles属性上的any扩展名。这同样适用于HTML。

{
    "beautify.onSave": true,
    "beautify.JSfiles": ["js", "json", "jsbeautifyrc", "jshintrc", "ts"],
    "beautify.HTMLfiles": ["htm", "html"]
}

这将在保存为TypeScript时启用美化,并且您可以在HTML选项中添加XML。

Linux上的Visual Studio代码:

按Ctrl +[取消缩进代码块和

按Ctrl +]使一个质量缩进

选择文本,右键单击选区,选择选项“命令调色板”:

打开一个新窗口。搜索“格式”,并根据需要选择有格式的选项。

不是这个。用这个:

菜单文件→首选项→工作空间设置 “编辑器。formatOnType”:真正的