Monday, February 7, 2011

lining up function parameter lists with vim

When defining or calling functions with enough arguments to span multiple lines, I want vim to line them up. For example,

def myfunction(arg1, arg2, arg, ...
               argsN-1, argN)

The idea is for argsN-1 to have its 'a' lined up with args1.

Does anyone have a way to have this happen automatically in vim? I've seen the align plugin for lining equal signs (in assignment statements) and such, but I'm not sure if it can be made to solve this problem?

  • I believe you have to type:

    :set cino=(0

    This is when using cindent of course.

    edit: I missed "set"

    rampion : I think you mean :set cino=(0
    From
  • The previous poster had it, but forgot the set

    :set cino=(0<Enter>
    

    From :help cinoptions-values

    The 'cinoptions' option sets how Vim performs indentation.  In the list below,
    "N" represents a number of your choice (the number can be negative).  When
    there is an 's' after the number, Vim multiplies the number by 'shiftwidth':
    "1s" is 'shiftwidth', "2s" is two times 'shiftwidth', etc.  You can use a
    decimal point, too: "-0.5s" is minus half a 'shiftwidth'.  The examples below
    assume a 'shiftwidth' of 4.
    
    ...
    
        (N    When in unclosed parentheses, indent N characters from the line
              with the unclosed parentheses.  Add a 'shiftwidth' for every
              unclosed parentheses.  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.
              (default 'shiftwidth' * 2).
    
                cino=                     cino=(0 >
                  if (c1 && (c2 ||          if (c1 && (c2 ||
                              c3))                     c3))
                      foo;                      foo;
                  if (c1 &&                 if (c1 &&
                          (c2 || c3))           (c2 || c3))
                     {                         {
    
    Commodore Jaeger : Typo: s/cinfo/cino/
    From rampion
  • Try the Align http://www.vim.org/scripts/script.php?script_id=294 and AutoAlign http://www.vim.org/scripts/script.php?script_id=884 scripts.

  • you might get some good mileage out of using a language-specific external tool as a Vim filter. for example, if you can write a Perltidy config file to generate the formatting you want (it looks like you would want the -lp -vtc=2 flags), you can then pipe your existing Vim buffer through it with

    :!/path/to/tidy -config /path/to/configfile
    

    if you're going to be running this sort of command frequently, you can define an command by putting something like the following in your .vimrc:

    command -range=% Tidy <line1>,<line2>!/path/to/tidy -config /path/to/configfile
    
    From hakamadare
  • I added

    set cindent
    set cino=(0
    

    to my vimrc and when it start a newline inside a set of parens, it indents me shiftwidth*2 instead of lining up to the parens. Any idea why?

    rampion : hmmm... it looks like maybe it's getting reset. Try doing the :set cindent :set cino=(0 commands from within vim and see if the behaviour is the same. If this fixes it, check your vimrc to see if anything could be reseting your cino setting.
    Luc Hermitte : Set (locally) these options in a python ftplugin, not in the .vimrc. ":versbose set" is your friend.

0 comments:

Post a Comment