VSCode:如何在保存时自动格式化?

nev*_*ves 6 formatting visual-studio-code vscode-settings

在Visual Studio Code中,保存文件后如何自动格式化源代码?

nev*_*ves 10

通过设置启用“保存时格式化”

"editor.formatOnSave": true
Run Code Online (Sandbox Code Playgroud)

您还可以仅针对一种特定语言进行设置:

"[python]": {
    "editor.tabSize": 4,
    "editor.insertSpaces": true,
    "editor.formatOnSave": true
},
Run Code Online (Sandbox Code Playgroud)

1.6.1版开始,Vscode支持“ 保存时格式化 ”。它将自动使用相关的已安装格式化程序扩展名来格式化整个文档。


Ben*_*ert 10

以下是在保存设置中更改 VS Code 自动格式的步骤:

  1. 使用 [Ctrl]+[Shift]+[p]
  2. 输入“首选项”
  3. 选择“首选项:打开用户设置”
  4. 搜索“格式”
  5. 更改“编辑器:保存时的格式”或“编辑器:粘贴时的格式”。

在 VS Code 中还有用于格式化的键盘快捷键。例如,格式化所选代码的默认值应为 [Ctrl]+K [Ctrl]+F(连续键入两个热键)。

以下是更改自动格式热键设置的步骤:

  1. 使用 [Ctrl]+[Shift]+[p]
  2. 输入“键盘”
  3. 选择“首选项:打开键盘快捷键”
  4. 搜索“格式”
  5. 更改“格式选择”或“格式文档”。


小智 9

转到/.vscode/settings.json文件并粘贴以下代码

{
    "editor.formatOnSave": true,
}
Run Code Online (Sandbox Code Playgroud)

它将在保存时格式化您的代码。