Visual Studio Code formatting for "{ }"
C++UbuntuVisual Studio-CodeLintC++ Problem Overview
I'm on Ubuntu. C++ in Visual Studio Code automatically lints like
if (condition == true)
{
DoStuff();
}
Instead I want to do :
if (condition == true) {
DoStuff();
}
How do I do that?
I've already installed the C/C++ extension from the marketplace.
C++ Solutions
Solution 1 - C++
base on @Chris Drew's answer
- Go Preferences -> Settings
- Search for C_Cpp.clang_format_fallbackStyle
- Click Edit, Copy to Settings
- Change from "Visual Studio" to
"{ BasedOnStyle: Google, IndentWidth: 4 }"
e.g.
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
- btw
ColumnLimit: 0
is helpful too, because google limit will break your code to next line when you do not need it.
If you want more:
- check https://clang.llvm.org/docs/ClangFormatStyleOptions.html
- customize your functionality to "C_Cpp.clang_format_fallbackStyle" for your loved favor.
More detail:
English: https://medium.com/@zamhuang/vscode-how-to-customize-c-s-coding-style-in-vscode-ad16d87e93bf
Solution 2 - C++
- Go File -> Preferences -> Settings
- Search for
C_Cpp.clang_format_fallbackStyle
- Change from "Visual Studio" to "LLVM", "Google" or "WebKit"
Solution 3 - C++
I generally have my own way of formatting almost everything :) so i prefer the most flexible way to achieve this. VS code is by far the most flexible editor as far as c++ formatting is concerned and also "easy".
This is what you should do to get custom formatting.
- create a file named .clang-format under the top folder of your work space.
- then start putting your configuration. you can refer page Clang format Style to know various options available.
- save the file and then either use Format Document (Ctrl+Shift+I) or Format Selection (Ctrl+K Ctrl+F)
Here is my file for your reference.
Standard: Cpp11
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 0
AccessModifierOffset: -4
NamespaceIndentation: All
BreakBeforeBraces: Custom
BraceWrapping:
AfterEnum: true
AfterStruct: true
AfterClass: true
SplitEmptyFunction: true
AfterControlStatement: false
AfterNamespace: false
AfterFunction: true
AfterUnion: true
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
SplitEmptyRecord: true
SplitEmptyNamespace: true
The formatting you are interested in especially is "AfterControlStatement: false"
Solution 4 - C++
Using MacOS for example, an ideal method of configuring clang-format
for VS Code is to first install clang-format with Homebrew:
brew install clang-format
Then, use it to export the full style settings to ~/.clang-format
:
clang-format -style=google -dump-config > ~/.clang-format
Then, perform the following in VS Code:
- Go to
Code/File -> Preferences -> Settings
and define the following parameters under User Settings: "C_Cpp.clang_format_path": "/usr/local/opt/llvm/bin/clang-format"
"C_Cpp.clang_format_style": "Google"
"C_Cpp.clang_format_fallbackStyle": "Google"
"C_Cpp.intelliSenseEngine": "Tag Parser"
This sets the formatter to the clang-formatter
installed with Homebrew, which will automatically pull your style settings from the ~/.clang-format
file you just created. This way, you can change every parameter in the style as desired and not just a subset of these.
The last parameter, C_Cpp.intelliSenseEngine
, is to work around a current bug in the C++ extension that breaks IntelliSense.
Solution 5 - C++
Install C# FixFormat extension
- View > Extension
- Search "C# FixFormat"
- Install
Shift + Alt + F
If it complains about multiple formatters, then press the Configure button and select C# FixFormat.
It is possible to go back to having open braces on a new line by going to File > Preferences > Settings. Then scroll down to Extensions, C# FixFormat configuration and uncheck Style > Braces: On Same Line
Solution 6 - C++
I haven't used Visual Studio in a while, but you should be able to open the Options menu through the Window tab.
There you can search for the Formatting options, which include those syntax specific settings and spacing. I think it's somewhere around the Text Editor options. The C/C++ extensions only installs the Visual C compiler and standard library, as well as the Windows SDK and a couple of other things.