如何在 VSCode 中自动缩进你的代码
VSCode 缩进
![](/data/attachment/album/202212/10/152929zff61fqq13kkpvy8.jpg)


代码中的缩进指的是你在代码行的开头处的空格。像其他代码编辑器和 IDE 一样，VSCode 允许你自动缩进你的代码。


你可以设置使用制表符或空格或任何你喜欢的缩进方式。


听起来不错吧？让我们来看看怎么做。


### 在 VSCode 中启用自动缩进


你有多种方法可以实现这个目标。在本指南中，我将向你展示三种在 VSCode 中自动缩进代码的方法。


#### 方法 1：配置全局用户设置


你可以通过命令模式访问全局用户设置。使用 `Ctrl + Shift + P` 来打开命令模式，搜索 `Open User Settings` 并按下回车：


![access user setting from command pallet in vscode](/data/attachment/album/202212/10/152931la5dsfwcdkfdqqp5.png)


它将打开设置。在那里，你需要搜索 `Auto Indent`，并在 “<ruby> 编辑器：自动缩进 <rt>  Editor: Auto Indent </rt></ruby>” 中选择 “<ruby> 全部 <rt>  Full </rt></ruby>”：


![enable auto indent from global user settings in vscode](/data/attachment/album/202212/10/152931w0i1ill441o01ol1.png)


接着自动缩进会被启用，并应用于 VSCode 中每个打开的文件。


#### 方法 2：在 VSCode 中使用检查器或格式化工具进行自动缩进


在这种方法中，你需要添加扩展程序，如代码格式化工具或者检查器，以获得理想的结果。


<ruby> 检查器 <rt>  Linter </rt></ruby>会识别代码中的错误，而<ruby> 格式化工具 <rt>  Formatter </rt></ruby>只对你的代码进行格式化，使其更具可读性。你可以在 [VSCode 市场](https://marketplace.visualstudio.com/vscode) 中搜索特定于你的编程语言的代码格式化器。


这里有一些我最喜欢的广泛流行语言的代码格式化工具和检查器：


* [C/C++](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)：适用于 C 和 C++ 编程语言。
* [PHP](https://marketplace.visualstudio.com/items?itemName=DEVSENSE.phptools-vscode)：适用于 PHP。
* [markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint)：适用于 Markdown 文件。
* [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python)：适用于 Python 编程语言。
* [ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)：适用于 JSON 和 javascript。
* [Beautify](https://marketplace.visualstudio.com/items?itemName=HookyQR.beautify): 适用于 JavaScript、JSON、CSS、SASS 和 HTML。


当你为你喜欢的编程语言添加了格式化工具，你可以按 `Ctrl + Shift + I` 来格式化代码。


同样地，你也可以使用命令模式做同样的事情。按 `Ctrl + Shift + P`，并搜索 `Format document`，然后按下回车。


![indent code in VSCode](/data/attachment/album/202212/10/152931sznu7d2nu9bss2uu.gif)


#### 方法 3：在保存文件时启用自动缩进功能


VSCode 允许你在保存你的代码时，通过一个小小的调整来格式化它。让我告诉你怎么做。


按 `Ctrl + ,`，它将打开用户设置提示。在那里，搜索 `Format On Save`。


![enable format on save option](/data/attachment/album/202212/10/152931v28qddopqfepwe2q.png)


从现在开始，当你保存文件时，你的文件将自动添加缩进。


### 总结


在本指南中，我解释了如何在 VSCode 中自动添加缩进。我建议使用第二种方法以获得更好的灵活性。


我希望你会发现本指南对你有帮助，如果你有任何疑问或建议，请在评论中告诉我。




---


via: <https://itsfoss.com/auto-indent-vs-code/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lkxed](https://github.com/lkxed) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

The indent in code refers to the space you have at the beginning of the code line.

**If you want to indent multiple lines at once, select them and press Ctrl+] to add the indent or Ctrl+[ to remove it. Pressing the same keys again will add (or move back) indent again.**

But this is the manual method. You have to identify and select the lines by yourself.

Like other code editors and IDEs, VS Code allows you to indent your code automatically.

You can set tabs or spaces or whatever you prefer for the indentation.

Sounds good? Let’s see how to do it.

## Enable automatic indent in VS Code

There are multiple ways you can achieve this. In this guide, I will show you three ways to indent your code in visual studio code automatically.

### Method 1: Configuring global user settings

You can access the global user settings via the command pallet. Use `Ctrl + Shift + P`

to open the command pallet and search for `Open User Settings`

and hit enter:

![access user setting from command pallet in vscode](https://itsfoss.com/content/images/wordpress/2022/11/access-user-setting-from-command-pallet-in-vscode.png)

It will open up the settings. From there, you will have to search for `Auto Indent`

and choose **Full** as an indent option in **Editor: Auto Indent**:

![enable auto indent from global user settings in vscode](https://itsfoss.com/content/images/wordpress/2022/11/enable-auto-indent-from-global-user-settings-in-vscode.png)

And the automatic indent is enabled and applied to every opened file in VSCode.

### Method 2: Using linter or formatter for automatic indent in VS Code

In this method, you will be required to add extensions such as a code formatter or linter to have the desired results.

Linters will identify the errors in code, whereas formatters will only format your code to make it more readable. You can search for code formatters in the [VSCode marketplace](https://marketplace.visualstudio.com/vscode?ref=its-foss) specific to your programming language.

And here are some of my favorite code formatters and linters for widely popular languages:

[C/C++](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools&ref=its-foss): For C and C++ programming language.[PHP](https://marketplace.visualstudio.com/items?itemName=DEVSENSE.phptools-vscode&ref=its-foss): For PHP.[markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint&ref=its-foss): For markdown files.[Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python&ref=its-foss): For Python programming language.[ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint&ref=its-foss): For JSON and javascript.[Beautify](https://marketplace.visualstudio.com/items?itemName=HookyQR.beautify&ref=its-foss): For javascript, JSON, CSS, Sass, and HTML.

Once you are done adding a formatter for your preferred programming language, you can press `Ctrl _ Shift + I`

to format the code.

Similarly, you can use do the same using the command pallet. Press `Ctrl + Shift + P`

to and search for **Format document**, and hit enter.

![indent code in VSCode](https://itsfoss.com/content/images/wordpress/2022/11/format-document-.gif)

### Method 3: Enable auto indent while saving the file

VSCode allows you to format your code while saving it with a little tweak. Let me show you how.

Press `Ctrl + ,`

and it will open the user settings prompt. From there, search for **Format On Save**:

![enable format on save option](https://itsfoss.com/content/images/wordpress/2022/11/enable-format-on-save-option.png)

And from now on, your files will add an indent automatically when you save them.

## Wrapping Up

Knowing and practicing the keyboard shortcuts help you work faster. Here are some interesting ones you should know.

[15 Useful Visual Studio Code Keyboard Shortcuts to Increase ProductivityThere is no doubt that Microsoft’s VS Code is one of the best open source code editor out there. Unlike the legendary Vim, VS Code doesn’t need you to be a keyboard ninja and has tons of features that developers swear by. But this doesn’t mean you](https://itsfoss.com/vs-code-shortcuts/)![](https://itsfoss.com/content/images/wordpress/2021/06/vs-code-shortcuts.png)


![](https://itsfoss.com/content/images/wordpress/2021/06/vs-code-shortcuts.png)

In this guide, I explained how you could add an indent automatically in VSCode. I would recommend using the second method for better flexibility.

I hope you will find this guide helpful and if you have any queries or suggestions, let me know in the comments.