我知道在 Visual Studio Code 中可以使用 Ctrl+F / Cmd+F 来格式化代码,但是如何为每种语言更改格式选项呢?
例如,在 Visual Studio 2013 中,我可以选择 CSS 的紧凑模式。
是否有另一个隐藏的 JSON 文件可以完成这个任务?
我知道在 Visual Studio Code 中可以使用 Ctrl+F / Cmd+F 来格式化代码,但是如何为每种语言更改格式选项呢?
例如,在 Visual Studio 2013 中,我可以选择 CSS 的紧凑模式。
是否有另一个隐藏的 JSON 文件可以完成这个任务?
2022更新
方案A:
按Ctrl+Shift+P
然后输入格式化文档为...
在列表底部点击配置默认格式化程序...
现在您可以从列表中选择您喜欢的美化器。
方案B:
进入文件 -> 首选项 -> 设置
,搜索格式
,
在左侧,单击文本编辑器
,右侧的第一项是编辑器:默认格式化程序
,从下拉菜单中选择之前安装的任何文档格式化程序。
如果我们现在谈论的是 Visual Studio Code,你可以在你的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
}
}
]
}
format-specific
should overwrite default
, but it does not. I have prettier
as default
, and autopep8
for python
, but only when I comment the default, autopep8 is used. If not: extension prettier cannot format <repo><file>
- Timo"editor.defaultFormatter": "dbaeumer.vscode-eslint"
,但我把我的 TypeScript 默认格式化程序改回默认格式化程序后,将其改回来就解决了问题:"[typescript]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" }
。这让我疯狂了,谢谢! - nkhileditor.defaultFormatter
的文档在哪里?我找不到它。而且,似乎设置值只允许您指定扩展名,而不能提供命令行工具? - bmitc在Visual Studio Code中美化javascript、JSON、CSS、Sass和HTML。
VS Code内部使用js-beautify,但它缺乏修改所需使用的样式的功能。此扩展程序使js-beautify在VS Code中运行,并尊重打开文件路径树中的任何.jsbeautifyrc文件以加载您的代码样式。使用F1 Beautify(美化选择)或F1 Beautify file(美化文件)来运行。
有关.jsbeautifyrc中设置的帮助,请参见Settings.md。
这是GitHub存储库:https://github.com/HookyQR/VSCodeBeautify.jsbeautifyrc
配置文件,这对于可能使用其他IDE编写代码的其他团队成员非常有帮助。对于Sublime Text,HTML-CSS-JS-Prettify插件是最好的选择。不幸的是,对于Eclipse,市场上的实现存在缺陷。我仍然需要使用editorconfig
,它可以做出不错的效果。 - Shiyaz我在2017年7月找到了一个适合我的解决方案,就是使用ESLint。众所周知,你可以以全局或本地的方式使用这个linter。我使用它作为本地的,并且与谷歌的样式指南一起使用。我设置的方式如下...
cd到你的工作目录
npm init
npm install --save-dev eslint
node_modules/.bin/eslint --init
我使用谷歌的样式和json配置文件
.eslintrc.json
的文件。您可以打开该文件并根据需要使用eslint规则进行修改。接下来,按cmd+,
以打开vscode
系统首选项。在搜索栏中键入eslint
,查找"eslint.autoFixOnSave": false
。复制该设置并粘贴到用户设置文件中,然后将false
更改为true
。希望这可以帮助使用vscode的某些人。要更改C#(OmniSharp)的格式设置,您可以使用json文件:
用户:~/.omnisharp/omnisharp.json
或 %USERPROFILE%\.omnisharp\omnisharp.json
omnisharp.json
文件。
示例:
{
"FormattingOptions": {
"NewLinesForBracesInMethods": false,
"NewLinesForBracesInProperties": false,
"NewLinesForBracesInAccessors": false,
"NewLinesForBracesInAnonymousMethods": false,
"NewLinesForBracesInControlBlocks": false,
"NewLinesForBracesInObjectCollectionArrayInitializers": false,
"NewLinesForBracesInLambdaExpressionBody": false
}
}
这篇文章详细介绍了此主题 | omnisharp.json模式文件(它已经在VS Code中,您只需使用CTRL+SPACE即可)
其他语言扩展可能也有类似的设置文件。