简体   繁体   English

如何在 Windows 上的 Visual Studio Code 中格式化代码

[英]How to format code in Visual Studio Code on Windows

How can I format code in Visual Studio Code in the last update on Windows.如何在 Windows 上的最后一次更新中格式化Visual Studio Code中的代码

I tried Ctrl + K , Ctrl + D doesn't work...我试过Ctrl + KCtrl + D不起作用...

I also tried Shift + Alt + F .我也试过Shift + Alt + F It didn't work either.它也没有用。

I tried the following solution, but that is not the issue:我尝试了以下解决方案,但这不是问题:

How do you format code in Visual Studio Code?你如何在 Visual Studio Code 中格式化代码?

If you are working on PHP you should download the PHP extension format code.如果您正在使用 PHP,您应该下载 PHP 扩展格式代码。

Press F1 and type:F1并键入:

ext install PHP Code Format

Then Shift + Alt + F will work.然后Shift + Alt + F将起作用。

You can also select the code you want to format and right click , find the format code menu item and it should have the keyboard command listed next to it.您还可以选择要格式化的代码并右键单击,找到格式代码菜单项,它旁边应该列出了键盘命令。

By default it should be Shift + Alt / Option + F .默认情况下它应该是Shift + Alt / Option + F

Simply use Ctrl + Shift + I只需使用Ctrl + Shift + I
Or或者
Right Click on document and Click on Format Document右键单击文档,然后单击格式文档

If it's for JavaScript, JSON, CSS, Sass , and HTML , there no better way than the Beautify extension .如果是针对JavaScript、JSON、CSS、 Sass和 HTML ,那么没有比Beautify 扩展更好的方法了。 It is simple and very useful!它很简单而且非常有用!

Once installed the way you do it are described here with good illustrations (to avoid content duplication): How can I format PHP files with HTML markup in Visual Studio Code?安装完成后,这里用很好的插图描述了您的操作方式(以避免内容重复): 如何在 Visual Studio Code 中使用 HTML 标记格式化 PHP 文件?

For others languages , you add the appropriate extensions and you can set keybinding (you need to get the appropriate command to execute, and add the keybinding to keybindings.json ).对于其他语言,您添加适当的扩展并可以设置键绑定(您需要获取适当的命令来执行,并将键绑定添加到keybindings.json )。 I have explained that in the link above .我已经在上面的链接中解释过了 Always read well the documentation of the extensions .始终阅读扩展的文档 There you can find all the possible configurations and so.在那里你可以找到所有可能的配置等等。

For specific languages you can search via Visual Studio Code in the extension marketplace .对于特定语言,您可以在扩展市场中通过 Visual Studio Code进行搜索 Enter just Beautify and you will see all the different Beautify extensions for the different languages.只输入Beautify ,您将看到不同语言的所有不同 Beautify 扩展。 Also you can use formatting format formatter keywords , like PHP formatting and so.您也可以使用格式化格式化关键字,如 PHP 格式化等。

Also, you can use the integrated formatting tool of Visual Studio Code .此外,您还可以使用Visual Studio Code集成格式化工具 Mostly by clicking right and selecting formatting document , you can also format just a selection.大多数情况下,通过单击右键并选择格式化文档,您也可以仅格式化选择。

Here is the shortcut for formatting the whole document:这是格式化整个文档的快捷方式:

  • On Windows: Alt + Shift + F在 Windows 上: Alt + Shift + F
  • On Mac: Shift + Option + F在 Mac 上: Shift + Option + F
  • On Ubuntu: Ctrl + Shift + I在 Ubuntu 上: Ctrl + Shift + I

As abdullam mentioned in his comment, the shortcut that worked for me is Shift + Alt + F .正如abdullam在他的评论中提到的,对我abdullam的快捷方式是Shift + Alt + F

(The accepted answer didn't work for me. I am using Visual Studio Code on a Mac. I couldn't find the extension mentioned above ie ext install PHP Code Format .) (接受的答案对我不起作用。我在 Mac 上使用 Visual Studio Code。我找不到上面提到的扩展,即ext install PHP Code Format 。)

Formatting any document in Visual Studio Code requires one to install a formatter plugin, like Prettier - Code formatted by Esben Petersen .在 Visual Studio Code 中格式化任何文档都需要安装一个格式化插件,比如由Esben Petersen格式化Prettier - Code

  • Install the plugin: Prettier - Code formatter from the Extensions window in Visual Studio Code.安装插件: Prettier - Visual Studio Code 的扩展窗口中的代码格式化程序

    • You can open the extensions window from the left aligned sidebar or simply by pressing Ctrl + Shift + X您可以从左侧对齐的侧边栏中打开扩展程序窗口,或者只需按Ctrl + Shift + X
  • Post installation, from the required page you can try the below安装后,您可以从所需页面尝试以下操作

    • Right click and choose Format Document ;右键单击并选择Format Document or或者
    • Try Alt + Shift + F on Windows在 Windows 上尝试Alt + Shift + F

The document should now be properly formatted.现在应该正确格式化文档。

  • PS: You can set "Prettier - Code formatted" as the default formatter for Visual Studio Code by performing the following: PS:您可以通过执行以下操作将“Prettier - Code formatted”设置为 Visual Studio Code 的默认格式化程序:
    • Open Settings by pressing Ctrl + , and search for editor.defaultFormatter .Ctrl + 打开设置然后搜索editor.defaultFormatter Choose esbenp.prettier-vscode from the list and Save.从列表中选择esbenp.prettier-vscode并保存。 By default, now formatting any document with the above steps will use Prettier as the default formatter.默认情况下,现在使用上述步骤格式化任何文档都将使用Prettier作为默认格式化程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM