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

33

11

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.

GladstoneKeep

Posted 2012-09-14T19:08:09.690

Reputation: 433

how do I do this in Atom text editor? – Saravanabalagi Ramachandran – 2019-03-15T17:05:19.167

Does simply inserting a tab or two work? – Darth Android – 2012-09-14T19:42:47.670

No, but I might be failing to understand. I've edited to clarify my question. – GladstoneKeep – 2012-09-14T20:53:03.183

Your edit describes what Space does, and to some extent Tab will to if your lines vary by a large amount, but if they're all pretty close then after two tabs your cursors should all be on a tabstop. You might have to do a bit of adjusting to get them all on the same tabstop, but it'll be less than trying to adjust with spaces. – Darth Android – 2012-09-14T21:21:17.217

It's weird. Pressing Tab at the end of each line indents the ragged multi-cursor insert out to a straight column, but it's not possible to achieve the same effect using regex. I searched for trailing spaces: \s*$ and replaced them with the tab character: \t - but \t does not achieve the same effect as pressing Tab manually. – amiregelz – 2012-09-14T22:38:07.320

1

FYI, I posted this as a feature request on Sublime Text 2 request board.

– GladstoneKeep – 2012-09-17T14:15:38.720

Answers

10

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.

d_rail

Posted 2012-09-14T19:08:09.690

Reputation: 2 809

I can't believe that this doesn't have a million up-votes. – Charles – 2016-11-17T22:06:26.607

1Wow, great! I installed the Sublime Alignment package, added "\n" (newline) to my "alignment_chars", and got the desired result. (Note: I didn't add it to "alignment_space_chars", which caused the lines to be deleted.) If you'll add that specific suggestion ("n") to your answer, I'll accept. – GladstoneKeep – 2012-10-02T17:23:40.947

Nice! Very useful package for perfectionists like me :) – amiregelz – 2012-10-04T20:52:15.173

3

There's also a similar package: Align Tabular, which is a little more flexible (you're able to align on multiple characters).

– d_rail – 2013-09-24T21:13:02.833

With AlignTab mentioned above by d_rail, remember to escape | by a single \\ – user – 2013-10-12T06:57:00.337

44

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.

kogre

Posted 2012-09-14T19:08:09.690

Reputation: 549

1I've been using this technique for a while but be careful, ctrl+shift+right doesn't treat all characters consistently so it may end up selecting more than just white space. – SpareBytes – 2015-06-12T16:37:59.040

1A really neat trick, I agree. But I just tried it without a character at the end, and used Shift-End Delete and it just worked. Does it work in some cases but not others? – Septagram – 2015-09-01T12:54:48.977

This is what I always do :) – Domino – 2016-04-09T21:23:55.667

This doesn't work if the c on one line is before the end of the word on another. Which is the problem I'm having right now. Certain lines don't contain enough character to scroll to the right, so when you do it wraps to the next line, leaving this hack as just that. This is a hack, not a useful catch-all replacement. – leetNightshade – 2016-05-25T01:52:34.677

2This looks twisted at first but it's a neat hack. Good for pasting columns with proper alignment. Next column can be pasted using text pastry plug-in. – user – 2014-02-26T18:05:40.620

0

For those who want to indent on characters other than = and |, I was able to install the Alignment package and modify the file in Sublime Text > Preferences > Package Settings > Alignment > Settings - User to look like the following:

{
  "alignment_chars": [
    "{create",
   ]
}

I then selected the following code:

  let!(:admin_user) {create(:admin_user, :admin)}
  let!(:another_admin_user) {create(:admin_user, :admin)}
  let!(:superadmin_user1) {create(:admin_user, :superadmin)}
  let!(:superadmin_user2) {create(:admin_user, :superadmin)}
  let!(:nonadmin_user) {create(:admin_user)}

and pressed Command + Ctrl + A to align it.

  let!(:admin_user)        {create(:admin_user, :admin)}
  let!(:another_admin_user){create(:admin_user, :admin)}
  let!(:superadmin_user1)  {create(:admin_user, :superadmin)}
  let!(:superadmin_user2)  {create(:admin_user, :superadmin)}
  let!(:nonadmin_user)     {create(:admin_user)}

Everything was moved to the leftmost column that the {create text could occupy while still being aligned.

Matthew Hinea

Posted 2012-09-14T19:08:09.690

Reputation: 101