In Sublime Text 2, how can I indent out to a straight column with multiple cursors on a ragged edge?

Suppose I've got multiple cursors along several lines, like this:

foo|
barr|
foobar|
baz|

How can I automatically push the whitespace at the end of each line out to a flat edge, like this?:

foo    |
barr   |
foobar |
baz    |

(In these examples, | is supposed to be my cursor.)

When you just Tab or Space from the initial arrangement, you get this:

foo    |
barr    |
foobar    |
baz    |

That's useful, but not what I'm looking for. I'm looking for some kind of keyboard shortcut that will let me indent from a ragged multi-cursor insert out to a straight column.


You could use wbond's Sublime Alignment

It may require you to add this to your settings file (Preferences>Package Settings>Alignment>Settings-User:

// The mid-line characters to align in a multi-line selection, changing
// this to an empty array will disable mid-line alignment
"alignment_chars": ["=", "|"],
// If the following character is matched for alignment, insert a space
// before it in the final alignment
"alignment_space_chars": ["=", "|"]

Replace "|" with whatever character you want to align.

Edit: As mtoast has found out, adding "\n" to "alignment_chars" gives the desired effect. Adding the new line character to "alignment_space_chars" is probably not needed and may delete the text. However, with my limited tests I did not see a difference.

Hold control and click at the end of each line. Then press the alignment hotkeys (For linux the default is ctrl + alt + a). You will find that the cursors are lined up with the furthest cursor position.

Edit2: Adding newline to "alignment_space_chars" will delete text if you highlight a block of text and Sublime Alignment can't find something else to align on the line (like a equal sign).

Also, if Sublime Alignment can't find something else to align, adding newline to "alignment_chars" will pad the end of lines with spaces (or tabs depending on your settings) to match longest line highlighted.


You can also do it without an external package using a minor hack, with only slightly more effort. Here's how:

| represents a cursor in these instructions.

  1. Create cursors on all desired lines using Ctrl+Click or by selecting a block of text and pressing Ctrl+Shift+L. (selection docs)

  2. Add additional spaces to the end of every word, until every cursor is at or past the point where you want your column (does not matter how far past):

    foo     |
    barr     |
    foobar     |
    baz     |
    
  3. (optional1) Type any character followed by a space; let's use c:

    foo     c |
    barr     c |
    foobar     c |
    baz     c |
    
  4. Press Home to make the cursors go the beginning of the lines:

    |foo     c
    |barr     c
    |foobar     c
    |baz     c
    
  5. Press → (right arrow) until the cursors are where you want your column:

    foo    | c
    barr   |  c
    foobar |    c
    baz    | c
    
  6. Press Ctrl+Shift+ to select all following whitespace and c, then press Delete:

    foo    |
    barr   |
    foobar |
    baz    |
    
  7. (optional1) Press Delete again to clean up the extra whitespace character that we added in the optional step.


1 We use the c character followed by a space only if there is additional content on the lines that we want to keep. Without it, Ctrl+Shift+ would select the first word of any additional content on the lines, making deletion tricky.