I am trying to align the following lines of code from this:

reg aaa;
reg [DATA_W-1:0] bbb;
reg ccc;

to this:

reg              aaa;
reg [DATA_W-1:0] bbb;
reg              ccc;

Using Tabular plugin. I have tried many regex sequences but cannot find a solution. The nearest one is:

:'<,'>Tab /\s\a

which produces

reg               a aa;
reg [DATA_W-1:0]  b bb;
reg               c cc;

Thus adding extra spaces between the matches.

  1. Is this white space inserted as default in Tabular plugin ?
  2. Is there a working solution (best regex) using Tabular plugin ?
  • 2
    Please don't edit your question to include the solution but scroll all the way down, paste your answer in the textbox and click "Post Your Answer". You will be able to accept your own answer after 48 hours. This will mark the question as solved and future readers will benefit from your solution. Thank you.
    – Friedrich
    Jun 18, 2023 at 20:55

2 Answers 2


This is the regex which almost works :

:'<,'>Tab /\s\a  

It is Equivalent to :

:'<,'>Tab /\s\a/l1  

which has the Default l1 , which indicates inserting 1 Space Character between the fields.

Solution thus requires overriding the Default l1 to l0 , which indicates that it should insert 0 Space Character between the fields :

(Original solution):

:'<,'>Tab /\s\a/l0  

Here is a reference :

  • 2
    How about adding a bit of explanation?
    – romainl
    Jun 19, 2023 at 5:57
  • 1
    It aligns word to the space + alpha character \s\a without inserting additional spaces \l0.
    – xyx
    Jun 19, 2023 at 11:35
  • 1
    Make it part of the answer and it will be perfect.
    – romainl
    Jun 19, 2023 at 11:46
  • 1
    I have tried to Elaborate the Answer , @romainl , I request you to Check My Edit.
    – Prem
    Jun 19, 2023 at 14:42

A regex-based desired solution is:

:%s/\(\%>3c\s\|^.*]\)/\1 /

\%>3c to positioning after 3th column (also not needed in this case), then \s to match a space, ^.*] to match lines that contains ] character... then, \1 to add extra spaces right the match.

then :%s/\%>17c\s\+// this erase spaces from column 17 till the text.

PD: you only must be aware of column numbers for correct alignment: 3c and 17c.

Best regards!

  • 2
    Welcome to Vi and Vim SE. This answer could be improved if you explained what your regex does and how it solves the problem.
    – Friedrich
    Jul 10, 2023 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.