1

I have the following options setup for C++ and Python development in vim:

autocmd BufEnter *.cpp setlocal tabstop=4 shiftwidth=4 softtabstop=4 textwidth=80 noexpandtab cindent cinoptions=:0,l1,t0,g0,(0
autocmd BufEnter *.py  setlocal tabstop=4 shiftwidth=4 softtabstop=4 textwidth=79 expandtab   cindent cinoptions=:0,l1,t0,g0,(0

In Python, when I am manually creating continuation lines (i.e. to keep code from spilling past a certain column number), auto-indenting and auto-alignment (i.e. visual selection, g,q), I encounter very different behavior for Python versus C++ code. For example:

Example: Works in C++ and Python Code.

val = some_class.some_function(this_is_a_variable_1, this_is_a_variable_2, this_is_a_variable_3, this_is_a_variable_4)

Becomes:

val = some_class.some_function(this_is_a_variable_1, this_is_a_variable_2,
                               this_is_a_variable_3, this_is_a_variable_4)

However, there is different behavior for C++ and Python code with the following example(s):

Example: Python Behavior (Desired).

If I insert a newline after the bracket ( character, the following continuation format occurs:

val = some_class.some_function(this_is_a_variable_1, this_is_a_variable_2, this_is_a_variable_3, this_is_a_variable_4)

Becomes:

val = some_class.some_function(
    this_is_a_variable_1, this_is_a_variable_2, this_is_a_variable_3,
    this_is_a_variable_4)

Example: C++ Behavior (Not Desired).

If I insert a newline after the bracket ( character, the following continuation format occurs:

val = some_class.some_function(this_is_a_variable_1, this_is_a_variable_2, this_is_a_variable_3, this_is_a_variable_4)

Becomes:

val = some_class.some_function(
                               this_is_a_variable_1, this_is_a_variable_2,
                               this_is_a_variable_3, this_is_a_variable_4)

This behavior is desirable for function prototypes/declarations, but not for general code.

Question

How do I get C++ auto-indentation/auto-formatting to behave the same as Python does (given the above examples) in VIM (i.e. have the continuation line use a single-tab or double-tab, rather than aligning with the opening bracket)?

Thoughts/Work So Far

Since the above indentation for C++ is only used for function declarations/prototypes, maybe I could write a macro to apply that specific indentation to a visual selection, and disable that formatting option in general.

Thank you.

1 Answer 1

1

I figured it out. From the documentation: (0,W8.

I found the following somewhat related answer on another SE site, to explain why I use a double-indent (i.e. (0,W8 instead of (0,W4) for continuation lines (i.e. Google and Oracle style guides encourage it).

WN    When in unclosed parentheses and N is non-zero and either
      using "(0" or "u0", respectively and the unclosed parentheses is
      the last non-white character in its line and it is not the
      closing parentheses, indent the following line N characters
      relative to the outer context (i.e. start of the line or the
      next unclosed parentheses).  (default: 0).

        cino=(0                    cino=(0,W4
          a_long_line(              a_long_line(
                      argument,         argument,
                      argument);        argument);
          a_short_line(argument,    a_short_line(argument,
                       argument);                argument);

Your Answer

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

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