如何更改 Visual Studio 代码中的格式设置选项?
How do you change the formatting options in Visual Studio Code?
我知道您可以使用 Ctrl+F / Cmd+[= 格式化代码Visual Studio 代码中的 17=]F 但是如何更改每种语言的格式选项?
比如Visual Studio 2013我可以选择CSS的紧凑模式。
是否有另一个隐藏的 JSON 文件可以做到这一点?
编辑:
现在支持(截至 2019 年)。请参阅 了解说明。
不,目前不支持 (2015年).
我刚刚在 Market Place 中找到了这个名为 beautify 的扩展,是的,它是另一个 config\settings 文件。 :)
Beautify javascript, JSON, CSS, Sass, and HTML in Visual Studio Code.
VS Code uses js-beautify internally, but it lacks the ability to
modify the style you wish to use. This extension enables running
js-beautify in VS Code, AND honouring any .jsbeautifyrc file in the
open file's path tree to load your code styling. Run with F1 Beautify
(to beautify a selection) or F1 Beautify file.
For help on the settings in the .jsbeautifyrc see Settings.md
这里是 GitHub 存储库:https://github.com/HookyQR/VSCodeBeautify
一个对我有用的解决方案(2017 年 7 月)是利用 ESLint。众所周知,您可以在全局或本地以多种方式使用 linter。我在本地使用它并使用 google 风格指南。我的设置方式如下...
cd to your working directory
npm init
npm install --save-dev eslint
node_modules/.bin/eslint --init
I use google style and json config file
现在您将在工作目录的根目录下有一个 .eslintrc.json
文件。您可以使用 eslint rules. Next cmd+,
to open vscode
system preferences. In the search bar type eslint
and look for "eslint.autoFixOnSave": false
. Copy the setting and pasted in the user settings file and change false
to true
. Hope this can help someone utilizing vscode.
打开该文件并根据需要进行修改
您可以对 "Settings" 进行一些更改。例如 javascript 规则以 "javascript.format" 开头。但是对于高级格式控制,还是需要用到一些扩展。
2022 年更新
方案一:
按Ctrl+Shift+P
然后输入Format Document With...
在列表末尾单击 Configure Default Formatter...
现在您可以从列表中选择您最喜欢的美化工具。
方案B:
去file -> preferences -> settings
搜索format
,
在左侧,单击 Text Editor
,右侧的第一项是下拉列表中的 Editor: Default Formatter
您可以选择您之前安装的任何文档格式化程序。
如果我们现在谈论 Visual Studio 代码 ,您现在 在 settings.json
中设置了默认格式化程序:
// Defines a default formatter which takes precedence over all other formatter settings.
// Must be the identifier of an extension contributing a formatter.
"editor.defaultFormatter": null,
指向任何已安装扩展的标识符,即
"editor.defaultFormatter": "esbenp.prettier-vscode"
您也可以这样做特定格式:
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[scss]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[sass]": {
"editor.defaultFormatter": "michelemelluso.code-beautifier"
},
还有.
您还可以在键盘快捷键 (keybindings.json
) 中为不同的格式化程序分配其他键。默认情况下,它显示为:
{
"key": "shift+alt+f",
"command": "editor.action.formatDocument",
"when": "editorHasDocumentFormattingProvider && editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly"
}
最后,如果您决定使用 Prettier 插件和 prettier.rc
,并且您希望 html、scss、json...
{
"semi": true,
"singleQuote": false,
"trailingComma": "none",
"useTabs": false,
"overrides": [
{
"files": "*.component.html",
"options": {
"parser": "angular",
"tabWidth": 4
}
},
{
"files": "*.scss",
"options": {
"parser": "scss",
"tabWidth": 2
}
},
{
"files": ["*.json", ".prettierrc"],
"options": {
"parser": "json",
"tabWidth": 4
}
}
]
}
要专门更改 C# (OmniSharp) 格式设置,您可以使用 json 文件:
用户: ~/.omnisharp/omnisharp.json
或 %USERPROFILE%\.omnisharp\omnisharp.json
工作区: omnisharp.json
OmniSharp 指向的工作目录中的文件。
示例:
{
"FormattingOptions": {
"NewLinesForBracesInMethods": false,
"NewLinesForBracesInProperties": false,
"NewLinesForBracesInAccessors": false,
"NewLinesForBracesInAnonymousMethods": false,
"NewLinesForBracesInControlBlocks": false,
"NewLinesForBracesInObjectCollectionArrayInitializers": false,
"NewLinesForBracesInLambdaExpressionBody": false
}
}
Details on this post | omnisharp.json schema(它已经在vscode中了,你可以直接CTRL+SPACE它)
其他语言扩展可能有类似的设置文件。
我刚才也发生了同样的事情。我在设置中将 prettier 设置为默认格式化程序,它又开始工作了。我的默认格式化程序是 null.
设置VSCODE默认格式化程序
文件 -> 首选项 -> 设置(针对 Windows)
代码 -> 首选项 -> 设置(对于 Mac)
搜索“默认格式化程序”。在下拉列表中,prettier 将显示为 esbenp.prettier-vscode.
我知道您可以使用 Ctrl+F / Cmd+[= 格式化代码Visual Studio 代码中的 17=]F 但是如何更改每种语言的格式选项?
比如Visual Studio 2013我可以选择CSS的紧凑模式。
是否有另一个隐藏的 JSON 文件可以做到这一点?
编辑:
现在支持(截至 2019 年)。请参阅
不,目前不支持 (2015年).
我刚刚在 Market Place 中找到了这个名为 beautify 的扩展,是的,它是另一个 config\settings 文件。 :)
Beautify javascript, JSON, CSS, Sass, and HTML in Visual Studio Code.
VS Code uses js-beautify internally, but it lacks the ability to modify the style you wish to use. This extension enables running js-beautify in VS Code, AND honouring any .jsbeautifyrc file in the open file's path tree to load your code styling. Run with F1 Beautify (to beautify a selection) or F1 Beautify file.
For help on the settings in the .jsbeautifyrc see Settings.md
这里是 GitHub 存储库:https://github.com/HookyQR/VSCodeBeautify
一个对我有用的解决方案(2017 年 7 月)是利用 ESLint。众所周知,您可以在全局或本地以多种方式使用 linter。我在本地使用它并使用 google 风格指南。我的设置方式如下...
cd to your working directory
npm init
npm install --save-dev eslint
node_modules/.bin/eslint --init
I use google style and json config file
现在您将在工作目录的根目录下有一个 .eslintrc.json
文件。您可以使用 eslint rules. Next cmd+,
to open vscode
system preferences. In the search bar type eslint
and look for "eslint.autoFixOnSave": false
. Copy the setting and pasted in the user settings file and change false
to true
. Hope this can help someone utilizing vscode.
您可以对 "Settings" 进行一些更改。例如 javascript 规则以 "javascript.format" 开头。但是对于高级格式控制,还是需要用到一些扩展。
2022 年更新
方案一:
按Ctrl+Shift+P
然后输入Format Document With...
在列表末尾单击 Configure Default Formatter...
现在您可以从列表中选择您最喜欢的美化工具。
方案B:
去file -> preferences -> settings
搜索format
,
在左侧,单击 Text Editor
,右侧的第一项是下拉列表中的 Editor: Default Formatter
您可以选择您之前安装的任何文档格式化程序。
如果我们现在谈论 Visual Studio 代码 ,您现在 在 settings.json
中设置了默认格式化程序:
// Defines a default formatter which takes precedence over all other formatter settings.
// Must be the identifier of an extension contributing a formatter.
"editor.defaultFormatter": null,
指向任何已安装扩展的标识符,即
"editor.defaultFormatter": "esbenp.prettier-vscode"
您也可以这样做特定格式:
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[scss]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[sass]": {
"editor.defaultFormatter": "michelemelluso.code-beautifier"
},
还有
您还可以在键盘快捷键 (keybindings.json
) 中为不同的格式化程序分配其他键。默认情况下,它显示为:
{
"key": "shift+alt+f",
"command": "editor.action.formatDocument",
"when": "editorHasDocumentFormattingProvider && editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly"
}
最后,如果您决定使用 Prettier 插件和 prettier.rc
,并且您希望 html、scss、json...
{
"semi": true,
"singleQuote": false,
"trailingComma": "none",
"useTabs": false,
"overrides": [
{
"files": "*.component.html",
"options": {
"parser": "angular",
"tabWidth": 4
}
},
{
"files": "*.scss",
"options": {
"parser": "scss",
"tabWidth": 2
}
},
{
"files": ["*.json", ".prettierrc"],
"options": {
"parser": "json",
"tabWidth": 4
}
}
]
}
要专门更改 C# (OmniSharp) 格式设置,您可以使用 json 文件:
用户: ~/.omnisharp/omnisharp.json
或 %USERPROFILE%\.omnisharp\omnisharp.json
工作区: omnisharp.json
OmniSharp 指向的工作目录中的文件。
示例:
{
"FormattingOptions": {
"NewLinesForBracesInMethods": false,
"NewLinesForBracesInProperties": false,
"NewLinesForBracesInAccessors": false,
"NewLinesForBracesInAnonymousMethods": false,
"NewLinesForBracesInControlBlocks": false,
"NewLinesForBracesInObjectCollectionArrayInitializers": false,
"NewLinesForBracesInLambdaExpressionBody": false
}
}
Details on this post | omnisharp.json schema(它已经在vscode中了,你可以直接CTRL+SPACE它)
其他语言扩展可能有类似的设置文件。
我刚才也发生了同样的事情。我在设置中将 prettier 设置为默认格式化程序,它又开始工作了。我的默认格式化程序是 null.
设置VSCODE默认格式化程序
文件 -> 首选项 -> 设置(针对 Windows) 代码 -> 首选项 -> 设置(对于 Mac)
搜索“默认格式化程序”。在下拉列表中,prettier 将显示为 esbenp.prettier-vscode.