Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tabular.vim : how to align on the first occurrence of 2 different delimiters placed at the beginning of Words?

Tags:

vim

tabular

I have installed the Tabular plugin, which works very well for me, as long as there are no complicated regexes involved…

But I have this list :

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

I wish to align it that way (on @… first, then on &…) :

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

which means I have 3 problems at the same time :

  • align on the first occurrence
  • of 2 different delimiters (@ and &)
  • which are not really delimiters but "special characters" at the beginning of Words

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

How should I proceed ?

like image 411
ThG Avatar asked Nov 11 '12 10:11

ThG


1 Answers

Align on the first occurrence

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

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

A little explaination:

  • ^ means the begin of the line
  • [^@]* match everything that isn't a @. The * means 0 or more times, as much as you can
  • \zs put the start of the regex here (everything from this point is matched)
  • @ the 'this point' in the previous sentence means the @ symbol
  • /l1l0 means align the 1st block to the left and add 1 space (l1) and align the 2nd block to the left and add 0 spaces (l0)

Align 2 different delimiters

You need to do this in 2 commands. To make your life easier you can name the pattern and use that name:

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

Now you can run

:Tabularize f_at
:Tabularize f_and

Map the commands

You can even map these methods to generate easy shortcuts. Read more about this here

like image 193
Wouter J Avatar answered Oct 12 '22 07:10

Wouter J