1

I want to align commens on multiple lines neatly, and found the following question and answer (I used the tabular package), but then I found out that it needs the comment specifier NOT to be shown on the same line.

For example, if it is the following line to align the comment:

call dein#add("vim-airline/vim-airline") " more info on the bottom 

It breaks as the align happens on the "vim-airline... part, not on the end of the line.

I also found this question and answer but it only work if only one comment specifier is shown before the actual one (in this case there are two, as it quotes the string).

Is it possible to only apply the alignment at the last element of a line?

1 Answer 1

2

With Tabular you can use:

'<,'>Tabular / ".\{-}$

Which will transform

call dein#add("scrooloose/nerdcommenter") " more info on the bottom
call dein#add("godlygeek/tabular") " more info on the bottom
call dein#add("tpope/vim-surround") " more info on the bottom

To

call dein#add("scrooloose/nerdcommenter")  " more info on the bottom
call dein#add("godlygeek/tabular")         " more info on the bottom
call dein#add("tpope/vim-surround")        " more info on the bottom

The pattern / ".\{-}$ will match a " character followed by as few as possible characters (.\{-}) until the end of the line $). If you don't use tabular but a similar plugin you should be able to use the same pattern.

2
  • Thanks. Do you know any way to define a shortcut of it? :command Align Tabular / ".\{-}$ caused an error when I saved the init.vim, but if I leave '<.'>, it does not allow me to use a range (using the v key's functionality).
    – Blaszard
    Commented Jun 14, 2018 at 5:28
  • @Blaszard You want to read :h :command-range
    – statox
    Commented Jun 14, 2018 at 7:14

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.