Search code examples
alignmentsublimetext3

How to align text with Alignment package in Sublime 3 with spaces around '='?


I want to use the Alignment package in Sublime Text 3 in order to align some variable assignments and I want to have spaces around the = character. This is an example:

bar = 'cherry'
bar_baz = 'tomato'
f = 'carrot'

This is how it turns out:

bar    = 'cherry'
bar_baz= 'tomato'
f      = 'carrot'

This is how it should be:

bar     = 'cherry'
bar_baz = 'tomato'
f       = 'carrot'

My settings for the Alignment package are the following:

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

Solution

  • The default settings show:

    :
    :
    // 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": ["="],
    :
    :
    

    Confirm yours haven't been mangled somehow.

    Using your input, if I select the text, or I use multiple cursors in front of =, and run the command it'll align as you specify, with spaces around =, like:

    bar     = 'cherry'
    bar_baz = 'tomato'
    f       = 'carrot'
    

    If I used multiple cursors, after running, the cursors are placed in front of the = still:

    bar     |= 'cherry'
    bar_baz |= 'tomato'
    f       |= 'carrot'
    

    So, the gist of this is, I can't duplicate your problem whether I change the alignment_chars or not.

    I will recommend looking at the AlignTab package as a replacement for Alignment as the "Live Preview Mode" is great.