Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim tabular only on the first match on the line?

I'm trying to format some python code with the tabular.vim plugin. It's currently a sqlalchemy declarative class, and looks something like this:

id     =  db.Column(db.Integer, primary_key=True)
status    =  db.Column(db.Integer, nullable=False, default=3)
...etc...

I'd like to be able to align only the very first equals sign in the list.

id     =  db.Column(db.Integer, primary_key=True)
status =  db.Column(db.Integer, nullable=False, default=3)
...etc...

Just a regular

: Tabularize /=

seems to match everything, and everything goes crazy.

Thanks very much in advance!

like image 230
Hoopes Avatar asked Jul 16 '12 02:07

Hoopes


3 Answers

You can use this command:

:Tabularize /^[^=]*\zs=

The pattern only matches the first =.


You can add these two line to ~/.vim/after/plugin/TabularMaps.vim

AddTabularPattern 1=    /^[^=]*\zs=
AddTabularPattern 1==   /^[^=]*\zs=/r0c0l0

Next time, simply run:

:Tabularize 1=

If you don't need spaces around =, run this:

:Tabularize 1==

like image 173
kev Avatar answered Nov 30 '22 23:11

kev


The suggestions above are good, but in this case they are a little too complicated and require too much typing. How about:

:Tab /=.*/

This works just fine -- match the first equal sign and everything after it, aligned left (default, which works just fine!).

like image 34
DigitalAce69 Avatar answered Dec 01 '22 00:12

DigitalAce69


Excellent plugin to do it: vim-easy-align.

like image 28
FlogFR Avatar answered Dec 01 '22 01:12

FlogFR