1

I often have code that looks something like this:

a = 1 # description 1
longer_name = 2 # description 2
# no_longer_needed = 3 # ignore
another_name = 4 # description 4

and would like to right-align the right-hand annotations, including the annotations of "commented-out" lines, so that it looks something like this:

a = 1                  # description 1
longer_name = 2        # description 2
# no_longer_needed = 3 # ignore
another_name = 4       # description 4

How can I do so with the Tabular plugin?

3

Just figured this out and wanted to share with people. You can use

:Tabularize /^\s*\S.*\zs#

where the ^\s*\S.*\zs# is a vim regexp that matches only the last comment character, while ignoring matches that occur as the first non-whitespace character in each line e.g. "commented out" lines (try searching/highlighting it).

If you want to put a handy remap in your .vimrc, for example to <Leader>., just use

vnoremap <expr> <Leader>. ':Tabularize /^\s*\S.*\zs' . split(&commentstring, '%s')[0] . "<CR>"
nnoremap <expr> <Leader>. ':Tabularize /^\s*\S.*\zs' . split(&commentstring, '%s')[0] . "<CR>"

where the above aligns by the "start-of-comment" character for an arbitrary filetype.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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