MisterEman22
MisterEman22

Reputation: 238

Keep else, catch, on same line as closing bracket in Visual Studio 2015?

I've managed to get Visual Studio to keep the opening bracket on the same line like this (the settings for this are under Formatting -> New Lines; the settings I am looking for, if they exist, are not in this area of options):

void foo() {
  bar();
}

but I can't figure out how to keep else and catch blocks on the same line as the ending bracket of the previous statement.

To clarify, this is what Visual Studio auto-formats to:

if (foo) {
  return 1;
}
else {
  return 2;
}

and this is what I want:

if (foo) {
  return 1;
} else {
  return 2;
}

Upvotes: 10

Views: 4670

Answers (1)

Christophe
Christophe

Reputation: 73465

Go to Tool->Option then General->Text editor->C++ -> Formatting -> New lines.

Choose the following settings:

  • Position of opening brace for control block: radio button for same line
  • Position of keyword: uncheck the else on new line.

The preview for this last setting will display

if (a < b)
{
} else
{
}

but this doesn't take into account the first option.

If you type an if/else statement, select the region, and ask the editor to reformat. The statement will appear as:

if (test) {
} else {
}

I could try this successfully on Visual Studio 2015. I tested it with a foreign language, so the english wording of above mentioned options might be slightly different, but close enough for being found.

Upvotes: 16

Related Questions