Robert Fraser
Robert Fraser

Reputation: 10927

Visual Studio : Automatically insert a space after typing if(

We have a code style checker that's run before every check-in that requires that C# if statements be formatted like:

if (condition)

However, my muscle memory has already developed for typing:

if(condition)

Is there any way to get Visual Studio 2010 to automatically insert that space if I type the if without it? I know you can set it that when you paste code, it will automatically do this, but not while typing. Thanks!

Upvotes: 16

Views: 21083

Answers (6)

Dheeraj Kumar
Dheeraj Kumar

Reputation: 13

Tools > Options > Text Editor > C# > Formatting > Spacing Then select all-> Press CTRL+K+D

Upvotes: 0

dotalchemy
dotalchemy

Reputation: 2467

Tools > Options > Text Editor > C# > Formatting > Spacing

Then check/tick Insert space after keywords in control flow statements, which is listed under the Set other spacing options sub group.

Win.

Upvotes: 28

JClaspill
JClaspill

Reputation: 1745

I have Visual C#, but as I recall it is the same in Studio.

  1. Click Tools
  2. Click Options
  3. Click 'Text Editor' on left menu
  4. Click 'C#' on left menu
  5. Click 'Formatting' on left menu
  6. Make sure all 3 options are checked in right area.
  7. Click 'Spacing' on left menu
  8. Click 'Insert space after keywords in control flow statements' in right area under the heading of 'Set other spacing options'.
  9. Click OK.

Upvotes: 1

Jesse McCulloch
Jesse McCulloch

Reputation: 683

Yeah, I think you are looking for Tools -> Options -> Text Editor -> C# -> Formatting -> Spacing -> Set other spacing options -> Insert space after keywords in control flow statements

Upvotes: 1

Town
Town

Reputation: 14906

My VS2010 does it by default after the closing bracket. Here's the option:

Tools > Options > Text Editor > C# > Formatting > General > Automatically format completed block on }

and also:

Tools > Options > Text Editor > C# > Formatting > Spacing > Set other spacing options > Insert space after keywords in control flow statements

Upvotes: 4

captncraig
captncraig

Reputation: 23108

Resharper will do this with its default formatting settings. Ctrl+K+D will format the entire document. Thats how I do it. It seems like VS's formatting options may be able to help you, but I have never played with them enough.

Resharper is totally worth it for this and so many other reasons.

Upvotes: 5

Related Questions