如何更改Visual Studio代码中的格式选项?

时间:2015-05-05 22:44:09

标签: visual-studio-code

我知道您可以在Visual Studio Code中使用ctrl + cmd + f格式化代码,但是如何更改每种语言的格式选项?

例如,在Visual Studio 2013中,我可以为CSS选择紧凑模式。

还有另外一个隐藏的json文件吗?

8 个答案:

答案 0 :(得分:10)

我刚在Market Place找到了名为beautify的扩展程序,是的,它是另一个配置\设置文件。 :)

  

在Visual Studio Code中美化javascript,JSON,CSS,Sass和HTML。

     

VS Code在内部使用js-beautify,但它缺乏能力   修改您想要使用的样式。此扩展程序可以运行   在VS Code中使用js-beautify,并且尊重任何.jsbeautifyrc文件   打开文件的路径树以加载代码样式。与F1 Beautify一起跑步   (美化选择)或F1 Beautify文件。

     

有关.jsbeautifyrc中设置的帮助,请参阅Settings.md

这是GitHub存储库:https://github.com/HookyQR/VSCodeBeautify

答案 1 :(得分:10)

在VS代码中

Ctrl+Shift+P

然后输入

Format Document With...

在列表末尾单击

Configure Default Formatter...

然后在下一步中选择您最喜欢的美容师

答案 2 :(得分:7)

不,目前不支持。

答案 3 :(得分:6)

您可以从"设置"进行一些更改。例如,javascript规则以" javascript.format"开头。但是对于高级格式控制,仍然需要使用一些扩展。

Rules settings for the format code command

答案 4 :(得分:5)

对我有用的解决方案(2017年7月)是利用ESLint。众所周知,您可以通过多种方式在全球或本地使用linter。我在本地和谷歌风格指南使用它。他们设置的方式如下......

  • 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打开该文件并进行修改。接下来cmd+,打开vscode系统偏好设置。在搜索栏中输入eslint并查找"eslint.autoFixOnSave": false。复制设置并粘贴到用户设置文件中,然后将false更改为true。希望这可以帮助某人使用vscode

答案 5 :(得分:1)

要专门更改C#(OmniSharp)格式设置,可以使用json文件:
用户~/.omnisharp/omnisharp.json%USERPROFILE%\.omnisharp\omnisharp.json
工作空间::指向OmniSharp指向的工作目录中的omnisharp.json文件。

示例:

{
  "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即可)

其他语言扩展名可能具有类似的文件进行设置。

答案 6 :(得分:0)

如果今天我们在谈论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"
},

see here


您还可以在键盘快捷键(keybindings.json)中为其他格式化程序分配其他键。默认情况下,它显示为:

{
  "key": "shift+alt+f",
  "command": "editor.action.formatDocument",
  "when": "editorHasDocumentFormattingProvider && editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly"
}

最后,如果您决定使用更漂亮的插件和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
            }
        }
    ]
}

答案 7 :(得分:0)

我刚才发生了同样的事情。我在“设置”中将“漂亮”设置为“默认格式化程序”,然后它再次开始工作。我的默认格式化程序为 null。

设置VSCODE默认格式化程序

文件->首选项->设置(对于Windows) 代码->首选项->设置(对于Mac)

搜索“默认格式化程序”。在下拉菜单中,漂亮的将显示为esbenp.prettier-vscode。

VSCODE Editor Option