How can I get Tabular.vim to only consider and affect lines that match /^item\s/, to use /\s\+/ as the delimiter, to left-align everything, and to use at least three spaces between words?

For example:

thing      ...
item Aaaaaaaaaaaaa "aaa"                    Bbb
  aaa             ...
item    Aaaa "aaaa"  Bbb

item   Aaa    "aaa" Bbb

should change to:

thing      ...
item   Aaaaaaaaaaaaa   "aaa"    Bbb
  aaa             ...
item   Aaaa            "aaaa"   Bbb

item   Aaa             "aaa"    Bbb

I've been trying to use AddTabularPipeline without success and with much confusion, and I'd like to throw my attempts out and figure out how the solutions here work.


Plugin EasyAlign

by Junegunn

I tried to give you an answer for Tabular.vim. However, I could only figure it out how to do it with vim-easy-align:

:%EasyAlign */\s\+/g/item\s/

I have used the shorthand notation for the filter option for the command :EasyAlign. The non-shorthand version looks like :%EasyAlign */\s\+/ { 'filter': 'g/item\s/'}.
More details about the filter option can be found here and under :h easy-align-filtering-lines.

Vim-easy-align provides a second way to use it, called the interactive mode:

<Visual mode>ga*<CTRL-F>g/\item\s/<CR><Space>                " visual mode
ga<Text Object or Motion>*<CTRL-F>g/\item\s/<CR><Space>      " operator mode

I am not entirely convinced the interactive mode is here more intuitive or easier to use.

I was hoping to use IMHO the more naive approach :g/item\s/EasyAlign */\s\+/<CR> but this does not work as well similar to Tabular.vim.

Note your question has been raised on stackoverflow and on the issue tracker of Tabular.vim:

There seems to be the undocumented function :GTabularize /pattern[/format] which, however, does not exactly do what you want. But was added to Tabular.vim after someone raised the wish to combine :global with :Tabularize.

Another vim alignment plugin is vim-lion which cannot help you here either.

:global applies [cmd] on each marked line separately

If I read :h :global correctly, the naive expectation, all filtered lines are provided for [cmd], is not true! I think this should be learned from this as well.

| improve this answer | |
  • Thanks for the recommendation of EasyAlign and the excellent example code. I've also found that the number of spaces at the narrowest width is configurable by using *\l0l0l2 in place of */\s\+/. Change the 2 to one less than how many spaces you want (e. g., if you want six spaces at the narrowest point, change it to 5). – Chai T. Rex Aug 31 '18 at 19:59

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.