My Quick Ways to Auto Format Code in VSCode


I don’t have much time to format my code to looks neat in VSCode (Ctrl + Shift + I), but the default formatting from Visual Code is not efficient for me. It makes the first bracket on function placed alone in one line. This makes my code longer!

If you’re used to using jump/goto line number it may not a problem. But for me, I need to take a screenshot of my codes for my report and this bracket is eating up the screenshot space. I tried googling around and finally found that this formatting can be changed.

Press ctrl + comma to open settings. Then search for “fallback“, you will find C_Cpp.clang_format_fallbackStyle like the picture below.

Visual Studio Formatting Settings

Change it from "Visual Studio" to { BasedOnStyle: Google, IndentWidth: 4 }

You can also add the ColumnLimit option to prevent your code break to the next line because of the Google-style column limit. For example:
{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0 }

So now, each time you have done so many edits in your code and you need to auto-format it, just press Ctrl + Shift + I to make it neat! 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *