vim tabular only on the first match on the line?

喜欢而已 提交于 2019-11-30 11:44:56

You can use this command:

:Tabularize /^[^=]*\zs=

The pattern only matches the first =.


You can add these two line to ~/.vim/after/plugin/TabularMaps.vim

AddTabularPattern 1=    /^[^=]*\zs=
AddTabularPattern 1==   /^[^=]*\zs=/r0c0l0

Next time, simply run:

:Tabularize 1=

If you don't need spaces around =, run this:

:Tabularize 1==

The suggestions above are good, but in this case they are a little too complicated and require too much typing. How about:

:Tab /=.*/

This works just fine -- match the first equal sign and everything after it, aligned left (default, which works just fine!).

Excellent plugin to do it: vim-easy-align.

Javid Jamae

As per this answer, instead of creating a static mapping for each case, you can do this dynamically by setting up a vim command like this:

command! -nargs=1 -range TabFirst exec <line1> . ',' . <line2> . 'Tabularize /^[^' . escape(<q-args>, '\^$.[?*~') . ']*\zs' . escape(<q-args>, '\^$.[?*~')

With this command, if you wanted to align based on the first = then you could do:

:TabFirst =

Or, if you wanted to align on the first { you could do:

:TabFirst {

This supports range selections as well as Tabularize's smart selection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!