Align text on an equals sign in vim

后端 未结 9 2444
一生所求
一生所求 2020-12-12 22:48

I tend to align code on equal signs for better readability. From this:

$ = jQuery.sub()
Survey = App.Survey
Sidebar = App.Sidebar
Main = App.Main


        
相关标签:
9条回答
  • 2020-12-12 22:56

    (From this answer in Vi & Vim Stack Exchange:)

    If you're in a pinch and want to get the expressions aligned, without having to install and learn any plug-ins, here is a quick way to do it.

    1. Select the lines on a visual selection. For example, if this is your whole buffer, you could use ggVG, but if these lines are in the middle of a file, just select appropriately. Perhaps V4j?
    2. Insert enough whitespace before the =, which you can do with :normal f=9i . (Note the "space" at the end.) This will add 9 spaces before each = in the lines of the visual selection. If 9 is not enough, add more (like 99 or 999, as much as you want.) Note that when you type : with the visual selection, Vim will automatically insert the range, so the actual command is :'<,'>normal f=9i , but you don't need to type those characters.
    3. Move to the column where you want the =s to be flushed to. In this case, line 2 has the longest variable name, so move to two spaces after the end of that name, which is where the =s should be at the end. If this is the whole buffer, you could use 2G2e2l to get there.
    4. Staying on that same column, move to the first line of the block. In this case, you're moving from line 2 to line 1, so k is enough.
    5. Start visual-block selection, pressing Ctrl-V.
    6. Move to the last line of the block. If this is the whole buffer, you could use G, if this is the middle of a file, you could use 4j to go four lines down, etc.
    7. Now you can use the < command to shift the lines left, but until they hit the left of the visual block. Each < will shift them by one 'shiftwidth' only, so you're likely to need more than one. So, to be sure, use 9< (or 99<, or 999<, if you added tons of spaces in step 2.)

    Voilà!

    This is a pretty cool technique and it can be helpful when you need more flexibility than plug-ins can afford you. It's a good one to learn and keep on your Vim toolbox.

    It is also quite flexible if you want to align on a different criteria other than a =, for example, to align on the third column (third word) of each line, use :normal 2W9i on step 2.

    0 讨论(0)
  • 2020-12-12 23:03

    You can use the Align Vim plugin to align such blocks, e.g. via typing

    vip:Align =
    

    in command mode, when the cursor is placed inside the to be aligned block.

    Where vip enters virtual mode and selects the current paragraph. The Align command is quite powerful, e.g. you can also specify multiple patterns, patterns are interpreted as regular expressions etc.

    0 讨论(0)
  • 2020-12-12 23:15

    An alternative plugin to Tabular:

    https://github.com/tommcdo/vim-lion

    From the docs:

    For example, glip= will turn

    $i = 5;
    $username = 'tommcdo';
    $stuff = array(1, 2, 3);
    

    into

    $i        = 5;
    $username = 'tommcdo';
    $stuff    = array(1, 2, 3);
    
    0 讨论(0)
提交回复
热议问题