Mayur Anvekar
Mayur Anvekar

Reputation: 23

Have Tabulize ignore some lines and align the others

I would want Tabulize to ignore lines which do not have a particular character and then align/tabularize the lines ..

text1_temp = text_temp;
temporary_line;
text2    = text_temp;

In the end i would like the following :

text1_temp = text_temp;
temporary_line;
text2      = text_temp;

// The 2nd "=" is spaced/tabbed with relation to the first "="

If i run ":Tabularize /=" for the 3 lines together I get :

text1_temp     = text_temp;
temporary_line;
text2          = text_temp;

Where the two lines with "=" are aligned with respect to the length of the middle line

Any suggestions .. ?

PS: I edited the post possibly to explain the need better ..

Upvotes: 2

Views: 232

Answers (2)

Peter Rincker
Peter Rincker

Reputation: 45107

I am not sure how to do this with Tabular directly. You might be able to use Christian Brabandt's NrrwRgn plugin to filter out only lines with = using :NRP then running :NRM. This will give you a new buffer with only the lines with = so you can run :tabularize/=/ and then save the the buffer (:w, :x, etc).

:g/=/NRP
:NRM
:tabularize/=/
:x

The easiest option is probably to use vim-easy-align which supports such behavior out of the box it seems. Example of using EasyAlign (Using ga as EasyAlign's mapping you):

gaip=

Upvotes: 2

Tamas Rev
Tamas Rev

Reputation: 7166

What about a simple replace, like :g/=/s/\t/ /g ?

If that doesn't work, you can try this too: :g/=/s/ \+= \+/ = /g

Explanation: The :/g/=/s will find all the lines that contain '=', and do the replacement for them. So, s/\t/ /g will replace tabs with spaces. These two things combined will do what you need.

Upvotes: 0

Related Questions