How to left-align IO stream operators << and >> in Vim?
For example, instead of following alignment:
std::cout << "Hello " << "Hello "
<< "world ";
I want left-align the <<
operator, as:
std::cout << "Hello " << " Hello "
<< "world ";
By default, Vim chooses the first one. Looks like it just increases the indentation by one level for the new line.
So, is the开发者_JAVA技巧re any way that I can get the second alignment by default?
P.S. I already tried the Align plugin, but it aligns the region in a table, like:
std::cout << "Hello World" << "Hello "
<< "World" << "World Hello".
which I consider too sparse.
I'm using Tabular and this works for me
:Tabularize /^[^<<]\S*
Output:
std::cout << "Hello World" << "Hello "
<< "world " << "World Hello";
Explanation
^
Beginning followed by <<
up to the to first <<
, then the match will start exactly at the first <<
.
With the Align plugin, the command for aligning selected lines of text the way you want is :<,>Align! l: <<
. The first argument is an AlignCtrl Command that tells it to left-align the first field and treat the rest of the line as a single field. The second argument is the separator. The Align manual explains all of the available arguments and pre-defined mappings.
精彩评论