Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 there 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.

like image 206
Fan Avatar asked Sep 15 '11 04:09

Fan


2 Answers

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 <<.

like image 199
Eric Fortis Avatar answered Nov 02 '22 04:11

Eric Fortis


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.

like image 22
Don Reba Avatar answered Nov 02 '22 04:11

Don Reba