Ray Chen

Ray Chen

          Student Backend Developer

© 2021

Dark Mode

Formatting Settings in Visual Studio 2019

Formatting settings in Visual Studio 2019 use K&R-derived layout and more.

K&R layout

For set K&R layout in Visual Studio 2019, from top menu: Tools -> Options -> Text Editor -> C/C++ -> Formatting options:

  • move open braces to a new line for namespaces

      namespace Namespace1
      {
          namespace Namespace2
          {
          }
      }
    
  • keep open braces on the same line for types

      namespace Namespace1
      class Class1 {
      };
    
  • move open braces to a new line for functions

      void Function1()
      {
      }
    
  • keep open braces on the same line for control blocks

      void Function()
      {
          while (cin >> word) {
              if (word.length() > 2) {
              }
          }
      }
    
  • keep open braces on the same line for lambadas

      void Function()
      {
          auto lambda1 = []() {
          };
          auto lambda2 = [&]() {
          };
      }
    
  • place braces on separate lines for scopes

      void Function()
      {
          // Scope
          {
              int i;
          }
      }
    
  • keep close braces on the same line for empty types, function bodies.

      class MyException
      {}
    
      MyClass::~MyClass()
      {}
    
  • place ‘catch’, ‘else’ and similar keywords on a new line

      try {
      }
      catch (...) {
      }
    
      if (a < b) {
      }
      else {
      }
    

    open braces

    close braces

Indent

For set indent in Visual Studio 2019, from top menu: Tools -> Options -> Text Editor -> Tabs options:

Indent

Tips: If you set the indentation to Keep tabs, then 8 spaces will be displayed on Github. You can add ?ts=4 to a diff or file URL will display tab characters as 4 spaces wide instead of the default 8. For more details and other nuggets, please see github-cheat-sheet

Line width

For set line width in Visual Studio 2019:

  1. From top menu: Extensions -> Manage Extensions options. (Ctrl + Shift + X)

  2. Search(Ctrl + E) editor Guidelines in the upper right corner and Download the editor Guidelines.

    editor Guidelines

  3. Close Visual Studio 2019 and start installing this extension.

    install

  4. For set line width, from top menu: View -> Other Windows -> Command Window options. Edit.AddGuideline and Edit.RemoveGuideline commands used to set the line width and remove the line width.

    i.e. to place a guide to the right of column 110, use Edit.AddGuideline 110.

    command

Reset the settings to default

For reset the settings in Visual Studio 2019, from top menu: Tools -> Import and Export Settings...options, you can also import or export settings.

reset