Tabular.vim: how to align the first occurrence of two different delimiters placed at the beginning of words?

I installed the Tabular plugin, which works very well for me if complex regular expressions are not involved ...

But I have this list:

one @abc @rstuvw &foo three @defg &bar four @mn @opq &kludge &hack twelve @hijkl &baz &quux 

I want to align it this way (on @ ... first, then on & ...):

 one @abc @rstuvw &foo three @defg &bar four @mn @opq &kludge &hack twelve @hijkl &baz &quux 

which means that I have 3 problems at the same time:

  • align on first occurrence
  • from two different delimiters (@ and &)
  • which are not really separators, but โ€œspecial charactersโ€ at the beginning of words

This is far beyond my understanding of both regular expressions and Tabular.vim

How should I proceed?

+4
source share
1 answer

Align on first occurrence

The help file explains this problem, you can use this command:

 :Tabularize /^[^@]*\ zs@ /l1l0 

A little explanation:

  • ^ means start of line
  • [^@]* matches all that is not @ . * means 0 or more times as far as you can
  • \zs puts the beginning of the regex here (everything from this point is mapped)
  • @ "this point" in the previous sentence means the @ symbol
  • /l1l0 means aligning the 1st block to the left and adding 1 space ( l1 ) and aligning the 2nd block to the left and adding 0 spaces ( l0 )

Align 2 different dividers

You need to do this in 2 teams. To make your life easier, you can name the template and use this name:

 :AddTabularPattern f_at /^[^@]*\ zs@ /l1l0 :AddTabularPattern f_and /^[^&]*\zs&/l1l0 

Now you can run

 :Tabularize f_at :Tabularize f_and 

Match the teams

You can even map these methods to create simple shortcuts. Read more about it here.

+7
source

Source: https://habr.com/ru/post/1445313/


All Articles