Robert Seifert
Robert Seifert

Reputation: 25232

How to align expressions between alignment regex in Sublime Text 3?

I have the following two lines in Sublime Text 3:

Rectangle(Maxwell, "MagnetsWEstart" , "r_ta - h_pm",  42,"NdFe35", True, "WE")
Rectangle(Maxwell, "MagnetsWEend", "r_ta", 1984.42, "NdFe35",False, "WE")

And I'd like to align all commata and the preceding text to the right:

Rectangle(Maxwell, "MagnetsWEstart", "r_ta - h_pm",      42, "NdFe35",  True, "WE")
Rectangle(Maxwell,   "MagnetsWEend",        "r_ta", 1984.42, "NdFe35", False, "WE")

I downloaded the Align Tab package and created the key-binding

{ "keys": ["ctrl+shift+a"], "command": "align_tab", "args" : {"user_input" : ","} }

The result is already close to what I want:

Rectangle(Maxwell , "MagnetsWEstart" , "r_ta - h_pm" , 42      , "NdFe35" , True  , "WE")
Rectangle(Maxwell , "MagnetsWEend"   , "r_ta"        , 1984.42 , "NdFe35" , False , "WE")

But how can I align the expressions between the commata to the right and also get red of the added space before the commas? Is this even possible?

Upvotes: 1

Views: 985

Answers (3)

ndnenkov
ndnenkov

Reputation: 36101

After aligning with Align Tab, match

[(,] *\K(.+?)( +)(?=,)

Replace with \2\1

Upvotes: 1

Robert Seifert
Robert Seifert

Reputation: 25232

The following steps inspired by ndn's answer were finally leading to the desired result:

1) Install reg_replace and Align tab package

2) Package: Reg_Replace -> Settings -> User:

{
    "replacements": {
        "commaRightRegex": {
            "find": "(\\s,)",
            "replace": ",",
            "greedy": true,
            "case": false
        }
    },
    "selection_only": true,
}

3) Tools -> New Plugin -> Include run_multiple.py

4) Key Bindings -> User -> add:

{ "keys": ["ctrl+shift+a"],
    "command": "run_multiple_commands",
    "args": {
        "commands": [
            {"command": "align_tab", "args" : {"user_input" : ",/r"}},
            {"command": "reg_replace", "args": {"replacements": ["commaRightRegex"]}}
        ]
    }
}

Which will finally enable the desired output with a single keyboard shortcut:

Rectangle(Maxwell, "MagnetsWEstart", "r_ta - h_pm",      42, "NdFe35",  True, "WE")
Rectangle(Maxwell,   "MagnetsWEend",        "r_ta", 1984.42, "NdFe35", False, "WE")

Upvotes: 0

ndnenkov
ndnenkov

Reputation: 36101

I found another solution. It turns out, Align tab has a built in option that you can add in your regex. Just align by regex and type ,/r. This means justify right.

Upvotes: 1

Related Questions