Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use vim's Tabularize plugin to only match the first occurance of a delimiter

Having been unable to find a linux based SQL beautifier to pipe my mySQL snippets to, I will settle for simply tabularizing my mySQL code. I want to turn this:

CREATE TABLE IF NOT EXISTS GROUP
(
   ID INT NOT NULL AUTO_INCREMENT,
   GROUPNAME VARCHAR(15) UNIQUE,
   ACTIVE BOOLEAN DEFAULT TRUE,

   PRIMARY KEY(ID)
)ENGINE=InnoDB

into this

CREATE TABLE IF NOT EXISTS GROUP
(
   ID         INT NOT NULL AUTO_INCREMENT,
   GROUPNAME  VARCHAR(15) UNIQUE,
   ACTIVE     BOOLEAN DEFAULT TRUE,

   PRIMARY KEY(ID)
)ENGINE=InnoDB

But if I do :Tabularize /\ I get

CREATE TABLE IF NOT EXISTS GROUP
(
        ID          INT           NOT        NULL         AUTO_INCREMENT,
        USERID      INT           NOT        NULL,
        GROUPNAME   VARCHAR(15)   UNIQUE,
        ACTIVE      BOOLEAN       DEFAULT    TRUE,

        PRIMARY     KEY(ID),
        FOREIGN     KEY           (USERID)   REFERENCES   USER(ID)
)ENGINE=InnoDB

How do I only align on the first space after the identifier (or alternatively, where do I find a command line SQL beautifier = ) ?

like image 650
puk Avatar asked Oct 29 '25 08:10

puk


1 Answers

To avoid Tabularize repeating your pattern to match other fields, you have to anchor it. At the beginning makes more sense in this case, so you can try something like this:

:Tab /^\s*\w*

There is only one field with this pattern in your line. But be careful — it might not happen in your example, but this matches the big majority of lines, thus it might be a good idea to apply Tabularize only in a range (either explicitly or by selecting the lines in visual mode).

like image 83
sidyll Avatar answered Oct 30 '25 23:10

sidyll