13

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?

4

4 回答 4

11

以前的海报有,但是忘记了set

:set cino=(0<Enter>

: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))
                 {                         {
于 2008-09-18T01:23:48.993 回答
7

我相信你必须发出命令:

:set cino=(0

这当然是在使用 cindent 的时候。

编辑:我错过了“设置”

于 2008-09-18T01:12:12.623 回答
2

试试 Align http://www.vim.org/scripts/script.php?script_id=294和 AutoAlign http://www.vim.org/scripts/script.php?script_id=884脚本。

于 2008-09-18T14:27:43.507 回答
1

使用特定于语言的外部工具作为 Vim 过滤器可能会获得一些好处。例如,如果你可以编写一个Perltidy配置文件来生成你想要的格式(看起来你想要-lp -vtc=2标志),然后你可以通过它来管道你现有的 Vim 缓冲区

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

如果你要经常运行这种命令,你可以通过在你的 .vimrc 中加入如下内容来定义一个命令:

command -range=% Tidy <line1>,<line2>!/path/to/tidy -config /path/to/configfile
于 2008-09-18T22:11:04.703 回答