Skip to content
Chris McGrath edited this page Dec 28, 2019 · 7 revisions

The general rule about code alignment is it finds the first instance of what you're looking for and aligns them. For the space this doesn't make sense as nearly every line you'll ever want to align starts with a space.

To get around this I introduced "align from caret" which is what "Align by space" uses. If you select the lines like you see here...

Image1

Then press align by space (or ctrl =, space) you'll get...

Image2

Moving the caret further over you can then do another alignment to get...

Image3

You can also use this with Code alignment's auto selection. In the last example, you can see the caret is between the A and the G in Age but nothing is selected. If you then did an align by space all the { would line up.

Second Explanation of Aligning Auto Properties

The following explanation was contributed by Michael Csikos.

public string FirstName { get; set; }  =>  public  string  FirstName { get; set; }
public string Surname { get; set; }    =>  public  string  Surname   { get; set; }
public int Age { get; private set; }   =>  public  int     Age       { get; private set; }
private Address Address { get;  set; } =>  private Address Address   { get; set; }

"Align by space" is the answer, albeit a multi-step process. In a nutshell, you're aligning each column by space, one column at a time. "Align by space" only aligns to the right of the current caret column position.

I normally click somewhere near the middle of the first column, lets say the caret is now on the second line, between the 'b' and 'l' in the word "public". Hit Ctrl+=, space.

Move the caret with the mouse or keyboard so that it is somewhere within the shortest word of the next column. In this case the word "int" is the shortest, so lets say the caret is now just after the 't'. Hit Ctrl+=, space.

Again , move the caret to the column that is within the smallest word of the next column: "Age". Hit Ctrl+=, space.

This takes a long time to read, but in practice it literally takes about four or five seconds to align three columns. And you might have 20 lines of auto properties.

Clone this wiki locally