Hans Deragon
Hans Deragon

Reputation: 562

Vim: Aligning columns separated by whitespace, but only for the first few columns, avoiding spaces in comments to be processed

Given the following text:

create table hivetable
(
  field1        string                  comment 'one comment #1 here',
  field2     string                   comment 'toto comment #2 here',
  field3        string,
  field4        string,
  field5  bigint    comment 'foobar comment #5 here',              
  field6          bigint         
)

How must I call Vim's Align script to get this final result:

create table hivetable
(
  field1 string  comment 'one comment #1 here',
  field2 string  comment 'toto comment #2 here',
  field3 string,
  field4 string,
  field5 bigint  comment 'foobar comment #5 here',              
  field6 bigint         
)

I guess that this would be a multi-step process. I would like to know for instance how to instruct Align to only align the two first columns and leave the rest as is.

create table hivetable
(
  field1 string                  comment 'one comment #1 here',
  field2 string                   comment 'toto comment #2 here',
  field3 string,
  field4 string,
  field5 bigint    comment 'foobar comment #5 here',              
  field6 bigint         
)

After that, a ":Align comment" and some manual editing, I will end with:

create table hivetable
(
  field1 string  comment 'one comment #1 here',
  field2 string  comment 'toto comment #2 here',
  field3 string,
  field4 string,
  field5 bigint  comment 'foobar comment #5 here',
  field6 bigint
)

Upvotes: 3

Views: 3819

Answers (2)

Meitham
Meitham

Reputation: 9670

I don't use Align but I use Tabular.

So in your case you want to align on the first character of the second column, the type here.

Assuming you have set hlsearch you can create a vim regex that captures these first characters with standard vim search, and visually confirm it by observing the highlight.

If you want to align per words, then your align pattern can simple be \w\+.

(
  field1  string   comment ' comment # 1  here ',
  field2  string   comment ' comment # 2  here ',
  field3  string ,
  field4  string ,
  field5  bigint   comment ' comment # 5  here ',
  field6  bigint
)

If you want to align only per the second column, your search pattern will have to be slightly more complex ^\s*\w\+\s\+\zs. The \zs means ignore the matching chars before my \zs symbol, highlight things from here forward (vim look ahead).

(
  field1  string                  comment 'comment #1 here',
  field2  string                   comment 'comment #2 here',
  field3  string,
  field4  string,
  field5  bigint    comment 'comment #5 here',
  field6  bigint
)

The point here is you form a regex that matches the column you're aligning on, by using vim search mechanism /pattern and using the highlight to visually confirm it is correct. You can then select the lines, with vi( and :'<,'>Tab /CTRL+R/ where CTRL+R means you literally press CTRL and R to retrieve a register, followed by a / the search register.

Upvotes: 2

After installing the plugin and reading the help by typing :help align I ended up by achieving that by setting the control alignment first:

:AlignCtrl =Clp1P1IW \S\+

All you have to do after that is select the block that you want to align by linewise-visual Shift+v and apply on it the command Align:

:'<,'>Align

and here it is the result:

create table hivetable
(
   field1  string   comment  'comment  #1  here', 
   field2  string   comment  'comment  #2  here', 
   field3  string, 
   field4  string, 
   field5  bigint   comment  'comment  #5  here', 
   field6  bigint  
)

If you want to align as discussed in the comment you need to run this command after the alignment control:

:'<,'>Align \(field\d\|string,\?\|bigint\|comment.*\)

Upvotes: 1

Related Questions