3

Consider editing the code

foo = long_function_name(var_one, var_two, var_three, var_four)

where the textwidth 'column' occurs just after var_two.

I would like to be able to press gqq on the line and it get formatted to:

foo = long_function_name(var_one, var_two, 
                         var_three, var_four)

Currently it does this:

foo = long_function_name(var_one, var_two, 
    var_three, var_four)

How can this be done?

4

Assuming you are editing a file recognized by Vim as source code and therefore Vim's more sophisticated indenting is enabled1 (check with :set cindent?) you can just do this:

:set cinoptions=(0

With :set textwidth=50 I get this formatting of your example line:

foo = long_function_name(var_one, var_two,
                         var_three, var_four)

cinoptions allows you to tune how Vim indents various code constructs.

The (N option is described, in part, in :h cino-( thusly...

(N    ... When N is 0 or the unclosed parentheses
      is the first non-white character in its line, line up with the
      next non-white character after the unclosed parentheses.

But for this particular case the help for WN (:h cino-W) actually has a (counter) example that is more in line with your case:

WN    When in unclosed parentheses and N is non-zero and either
      using "(0" or "u0" ...

    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);

We want it to look like the example on the left so cinoptions=(0.

Take a look at :h cinoptions-values for heaps of other ways to tune the indentation.

1 This doesn't apply to all languages. See the comments/chat for discussion about Python files having nocindent by default.

  • This doesn't work for me but I may be missing something. I have :set cindent? returning cindent and :set cinoptions? returning cinoptions=(0. Is there something else I am missing to get it to work? – rwolst Apr 10 '18 at 12:28
  • What do you see when you use gqq after setting this? – B Layer Apr 10 '18 at 12:30
  • 1
    I don't use Python so not quite sure why but Vim doesn't set cindent automatically when I open a .py file...Okay, I found this: stackoverflow.com/questions/16570737/… See if that helps you. – B Layer Apr 10 '18 at 12:47
  • 1
    Thanks for the help, I found a Python fix if I run setlocal cino=(0. I would be really interested in what is going on that means I have to use this. This thread is relevant but I don't completely understand it vim.1045645.n5.nabble.com/…. – rwolst Apr 10 '18 at 12:52
  • 1
    Oh, you know, since indentation/formatting matters in Python maybe they figured having a lot of customizability isn't a good default configuration. – B Layer Apr 10 '18 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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