Search code examples
vimalignmentverilog

vim align columns using Align or Tabularize (Verilog)


I am trying to use either Align or Tabularize to take the following code:

   // UDW
   input  [UDW_WIDTH-1:0]     udw_data;
   input [TYPE_WIDTH-1:0]     udw_type;
   input                      udw_valid;
   output                     udw_ready;
   // UniClkPktGen
   input [UTW_DATA_W-1:0]     utw_data_in;
   input                      utw_valid_in;
   input [FREQOFFSET_W-1:0]   freqOffset;
   input [ACCDELAY_W-1:0]     accDelay;
   // RIF                     
   input [31:0]                    rif_add_bus;
   input [31:0]                    rif_data_wr;
   input [3:0]                  rif_byte_en;
   input                         rif_wr;
   input                         rif_rd;
   output [31:0]                   rif_data_rd;
   output                        rif_ack;

To be aligned to this:

   // UDW
   input  [UDW_WIDTH-1:0]     udw_data;
   input  [TYPE_WIDTH-1:0]    udw_type;
   input                      udw_valid;
   output                     udw_ready;
   // UniClkPktGen
   input  [UTW_DATA_W-1:0]    utw_data_in;
   input                      utw_valid_in;
   input  [FREQOFFSET_W-1:0]  freqOffset;
   input  [ACCDELAY_W-1:0]    accDelay;
   // RIF                     
   input  [31:0]              rif_add_bus;
   input  [31:0]              rif_data_wr;
   input  [3:0]               rif_byte_en;
   input                      rif_wr;
   input                      rif_rd;
   output [31:0]              rif_data_rd;
   output                     rif_ack;

As you can see, sometimes there will be square brackets before the variable name, some time won't. Comments could be in the middle, I would like them to be ignored. I want to keep the space before the input/output. I tried some regex, but couldn't manage to do it.


Solution

  • Ok I have managed to do it with easy-align (thanks Doktor OSwaldo). I used the advance examples : https://github.com/junegunn/vim-easy-align/blob/master/EXAMPLES.md

    I took this code and added it to ~/.vim/autoload/easy_align.vim (taken from EXAMPLES.md above).

    let g:easy_align_delimiters['d'] = {
    \ 'pattern': ' \ze\S\+\s*[;=]',
    \ 'left_margin': 0, 'right_margin': 0
    \ }
    

    I don't fully understand the pattern so I will not try to explain it. But it works :)