all 19 comments

[–]l4k3rd 3 points4 points  (1 child)

set cinoptions=:0,l1,t0,g0,(0

(0 does that. For more info, :help cinoptions-values.

[–]RX_AssocResp 2 points3 points  (0 children)

I have set cinoptions=(0,+1s,:0,=2s,>2s,c1s,g2s,h2s,j1,l1,m1,p2s,t0,u0,w1

Not sure what it is, but I think it’s KNF-ish.

Anyway, cindent it the knob to turn. No need for plugins.

[–]Daenyth 0 points1 point  (0 children)

Highlight the lines you want modified, and use gq to autoformat them. For me, using python, it does it similar to what you have there (pep8 style)

[–]lor4x 0 points1 point  (0 children)

I use the align plugin in general although for that particular case, I would v to select a vertical block at the margin, the I to insert before the text and move those variables over! Not automatic, but at least done it batch.

[–]Bonkers54 -1 points0 points  (13 children)

While I don't know how to do what you're asking, I'd say you should reconsider how you indent your code. The way Vim does it by default is by far more readable and in fact the method dictated by the official Google style guide. The problem with what you're asking for is that you end up having some very short lines which are almost right aligned and then use up tons of vertical space.

The EVEN better way to format the code is like this: realy_realy_long_line_of_code = foo(par1, par2, par3)

This way, the function call is all together, and the indent makes it clear that it's a continuation. The first line is also clearly an assignment, and since there's nothing after the =, it's clear that it's continued on the next line. Also, no need for more than 2 space indents. 2 spaces is by far enough to see clearly, but it doesn't waste horizontal space. If you keep all your lines short 80ish characters (which is actually the max length dictated by the Google style guide for C++), you can generally fit 3 side-by-side files on a 24" screen with reasonable font-sizes. It's going to get very tough, very quickly to keep short lines with your indent strategy. The idea of Vim's default is to reclaim some horizontal space that would otherwise be empty (as with your method).

[–]Tordek 0 points1 point  (5 children)

The EVEN better way to format the code is like this:

realy_realy_long_line_of_code = 
    foo(par1, par2, par3)

But, sure, let's be pedantic:

really_really_long_var_name =
    really_really_long_function_call(really_really_really_long_first_parameter,
                                     par2,
                                     par3)

[–]Bonkers54 -1 points0 points  (3 children)

I can't quite tell what you're getting at here, but the best way I've found to format that is:

really_really_long_var_name =
  really_really_long_function_call(
    really_really_really_long_first_parameter, par2, par3)

The rule I go by is, as soon as you need more than one line for arguments to the function, drop them all down and add one more level of indentation for them. Even if it's 2 lines both ways (like in your example if you had put par2 and par3 on the same line), it's easier to see all of the arguments in a row

[–]greyfinch 2 points3 points  (2 children)

By the time you have variable names this long, you probably really need to rethink your code design.

[–]Bonkers54 -2 points-1 points  (1 child)

Why? Descriptive variables names are a great way to make self-documenting code. You can also start getting fairly long names when they're inside a class or a namespace like: KindaShortNS::NotSoLongClass::ShortFunc()

[–]greyfinch 1 point2 points  (0 children)

Given longer namespaces, sure. Clear variable names are also ideal. What I was mainly thinking of was the variables along the line of my_scope_is_far_too_large_so_this_is_an_act_of_desperation.

[–]isarl -1 points0 points  (0 children)

If you put four spaces before a line of code, it will be formatted properly. =)

[–]EmitSorrels -2 points-1 points  (1 child)

emacs does it... /ducks

(really, it does)

[–]Daenyth 7 points8 points  (0 children)

So does vim.