Search code examples
vimiostreamindentationauto-indent

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.


Solution

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