简体   繁体   English

如何在 Visual Studio Code(热键)中格式化文件?

[英]How to format file in Visual Studio Code (hotkeys)?

How to format file |如何格式化文件 | project folder |项目文件夹 | all project files (using hot keys AND using menu) in Visual Studio Code? Visual Studio Code 中的所有项目文件(使用热键和使用菜单)?

If you're on Windows use Shift + Alt + F to make your code "pretty". 如果您使用的是Windows,请使用Shift + Alt + F将代码设置为“漂亮”。

(This is similar to Ctrl + K + D in Visual Studio) (这类似于Visual Studio中的Ctrl + K + D

You will have to do this for each individual file though. 但是,您必须为每个单独的文件执行此操作。 I don't know of a way to format all files at once. 我不知道一次格式化所有文件的方法。

试试这个热键

Ctrl + Shift + i

@Sergii @Sergii

Your question looks like you are looking for a way to format your entire code in VSCode. 您的问题看起来像是在寻找一种在VSCode中格式化整个代码的方法。 If this is the case, a VSCode extension like Prettier should meet the three requirements you've listed (file | project folder | all project files). 如果是这种情况,像Prettier这样的VSCode扩展应满足您列出的三个要求(文件|项目文件夹|所有项目文件)。 Bear in mind though that formatting all project files is a bit unclear and you probably want to be specific about formatting all *.ts or all *.js files etc. 请记住,格式化所有项目文件有点不清楚,您可能想要具体格式化所有* .ts或所有* .js文件等。

The below may be of help to you: 以下内容可能对您有所帮助:

  • Read about prettier because it provides ability to format entire code base 阅读更漂亮,因为它提供了格式化整个代码库的能力
  • Install prettier-vscode extension to your VSCode and enable settings that you want prettier-vscode扩展安装到VSCode并启用所需的设置
  • Alternatively, you can use prettier from the CLI, npm build script or via its API in build task - the choice is yours. 或者,您可以使用CLI,npm构建脚本或构建任务中的API更漂亮 - 您可以选择。

The one I found most useful is adding it as a git pre-commit hook for staged files. 我发现最有用的一个是将它添加为阶段文件的git pre-commit钩子。

I hope above points you in the right direction. 我希望上面指出你正确的方向。

SOJ SOJ

In case you are not able to format your code with any of these commands it's maybe because you did not set up your formatter configuration如果您无法使用这些命令中的任何一个来格式化您的代码,那可能是因为您没有设置格式化程序配置

I recommend you to use Prettier (install from Extensions)我建议您使用 Prettier(从扩展安装)

  1. open the command palette ( CTRL + SHIFT + P - on windows)打开命令面板( CTRL + SHIFT + P - 在 Windows 上)
  2. search for prettier: Create Configuration File搜索prettier: Create Configuration File
  3. hit Enter and choose your project directory folder path点击 Enter 并选择您的项目目录文件夹路径
  4. if you can see a .prettierc file added into your directory, you must be able to use SHIFT + ALT + F to format your code如果你可以看到一个.prettierc文件添加到你的目录中,你必须能够使用SHIFT + ALT + F来格式化你的代码

+1. +1。 also you are able to set your environment to automatically format your file at save.您还可以将环境设置为在保存时自动格式化文件。
You can do it writing this line in settings.json:你可以在 settings.json 中写下这一行:

"editor.formatOnSave": true

Hint: Access it using the command palette again and writing settings.json, and select提示:再次使用命令面板访问它并写入 settings.json 和 select
Preference: Open Settings (JSON)

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

相关问题 在Visual Studio Code中格式化组件属性的下一行 - Format component attributes next line in Visual Studio Code 如何在Visual Studio Code中使用Typescript进行TDD? - How to do TDD with Typescript in Visual Studio Code? 如何使 Visual Studio Code Angular 友好 - How to make Visual Studio Code Angular friendly 在终端中有一个文件可见,但在Visual Studio代码中没有文件结构 - there are a file visible by terminal but not in file structure in visual studio code 如何在Mac上使用Visual Studio Code将Azure发布设置文件导入Angular项目? - How to import an Azure publishsettings file to an Angular project using Visual Studio Code on mac? 在 Visual Studio Code 中转到角度 html 文件中的定义 - Go To Definition in angular html file in Visual Studio Code 如何在visual studio代码中有效地遍历angular代码 - How to effectively traverse angular code in visual studio code 如何使用Visual Studio Code将Visual Studio 2015中开发的Angular 4项目用于Angular 6? - How to use Angular 4 project developed in Visual Studio 2015 to Angular 6 using Visual Studio Code? 如何阻止 npm 编译 Visual Studio 代码中的每个击键? - How to stop npm from compiling every keystroke in visual studio code? 如何在Visual Studio代码中调试Angular2 TypeScript应用程序? - How to debug an Angular2 TypeScript application in Visual Studio Code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM