How to align texts in Sublime Text 3?
Asked Answered
C

2

32

I use Sublime Text 3 Plug-in called AlignTab to align my code like this:

 Version : 1.4.1
 Author  : Ken Wheeler
 Website : http://kenwheeler.github.io
 Docs    : http://kenwheeler.github.io/slick
 Repo    : http://github.com/kenwheeler/slick
 Issues  : http://github.com/kenwheeler/slick/issues

Now, I want to my texts to stay next to the colon like this:

 Version: 1.4.1
  Author: Ken Wheeler
 Website: http://kenwheeler.github.io
    Docs: http://kenwheeler.github.io/slick
    Repo: http://github.com/kenwheeler/slick
  Issues: http://github.com/kenwheeler/slick/issues

How do I accomplish that ?

Custard answered 10/4, 2015 at 1:43 Comment(2)
I just do not get you guys. How is this question and how are the answers opinionated or spam?Itch
@t1. I edited it so that it meets the guidelines, but I can't initiate a reopen vote until 3000 pointsTanning
T
32

You can do this with AlignTab, no need for a different plugin. You just need to use the more advanced regex functionality as described on the GitHub page.

Bring up the Command Palette (Ctrl+Shift+P on Windows or cmd+shift+P on Mac), type in "AlignTab", press enter, and type this in and hit enter:

:/r0clf1

Props to @Hank for including 0 spaces option, didn't even realize he didn't want the space.

Explanation:

  • the : finds the colon
  • the / says okay now here come some arguments about what I want you to do with the colon
  • the r means right-justify the first column
  • the 0 means 0 spaces after the right column
  • the c means center the middle column (which is just the colon, so it doesn't do anything)
  • the l means left-justify the right column
  • the f1 means only do this for the first match on the line
Tanning answered 10/4, 2015 at 4:32 Comment(2)
How would you go about saving this in AlignTab's preferences so I can just access it from the right click menu?Stockholder
Note that you must first install via using Command Palette and Install PackageHaiphong
F
9

The AlignTab docs or the linked examples have enough info to cover what you need.

If you highlight the original text, then Cmd + Shift + P (or Ctrl Shift P on windows) and enter AlignTab Live Preview mode, you can enter a regex and use the special rlc syntax.

The syntax is:

\s+:/r0c1l

The regex are \s+ (greater than 1 spaces) to the first :, then r0 (align right, 0 spaces), c1 (center mark with 1 padding to the right), then l (left align the remaining text). There's no need for an 'f1' at the end of the expression since the regex matches the leading spaces before your first colon (per your example).

If you had newly written text without the pre-formatting that you've already done, like:

Version: 1.4.1
Author: Ken Wheeler
Website: http://kenwheeler.github.io
Docs: http://kenwheeler.github.io/slick
Repo: http://github.com/kenwheeler/slick
Issues: http://github.com/kenwheeler/slick/issues

Then to convert to your desired format below, will be:

:/r0c1lf1

Converted below:

Version: 1.4.1
 Author: Ken Wheeler
Website: http://kenwheeler.github.io
   Docs: http://kenwheeler.github.io/slick
   Repo: http://github.com/kenwheeler/slick
 Issues: http://github.com/kenwheeler/slick/issues

This result is actually better than your original question text, because there's no leading space.

P.S. I found that the Tablular examples from the AlignTab docs to be slighty more helpful, although it doesn't represent the real implementation of AlignTab.

Feculent answered 10/4, 2015 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.