0

Here is an example of line continuation:

ThisIsASuperLongClassNameWithALotOfExtraCharacters thisIsALongVariableName =
    ThisIsAModeratelyLongSuperClass.longFunctionCall(
        thisIsAVeryLongVariableNameThatCanSpanAFullLineByItself);

Here is an example of an indented block:

if (x == 5) {
  fetchBob() 
}

Typically, for all the settings of cindent and smartindent I've tried so far, Vim treats these two cases identically and adds the same amount of space in each case. To be explicit, I am referring to both the behavior when pressing Enter in insert mode, and also the behavior when pressing = in Visual mode.

Is there some magic combination of cindent and smartindent or another setting that will cause vim to automatically add 4 spaces in the line continuation case and 2 spaces in the indented block case?

1 Answer 1

1

I am referring to both the behavior when pressing Enter in insert mode, and also the behavior when pressing = in Visual mode.

The "equal" command depends on :h 'equalprg' option value. If it's set then Vim invokes an external formatter program, and all those indents will be set according to that program's configuration. In fact, you may end up doing this, as there are many good C/C++ formatters, while Vim's one is far from perfect.

Is there some magic combination of cindent and smartindent or another setting

It's called cinoptions. As far as I can tell, you may want setl cino=e-2. Read :h cinoptions-values for more info.

3
  • Thanks for the pointer, I'll have to check the help pages. setl cino=e-2 seems to introduce only two spaces after the = in my first example, rather than 4 as desired.
    – merlin2011
    Jun 24, 2020 at 7:13
  • It turns out that the setting I needed was set cino=+4.
    – merlin2011
    Jun 24, 2020 at 7:16
  • 1
    Ah, I think I finally understand. Your answer is correct iff shiftwidth=4, but I had shiftwidth=2 so your answer did not have the intended effect!
    – merlin2011
    Jun 24, 2020 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.