用vim排列函数参数列表

时间:2020-03-06 14:19:32  来源:igfitidea点击:

当使用足够多的参数定义或者调用函数以跨越多行时,我希望vim将它们对齐。例如,

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

这个想法是让argsN-1的'a'与args1对齐。

有没有人有办法让它在vim中自动发生?我已经看到align插件用于衬里等号(在赋值语句中)等,但是我不确定是否可以解决该问题?

解决方案

我相信我们必须发出命令:

:set cino=(0

当然是在使用cindent的时候。

编辑:我错过了"设置"

以前的海报有它,但是忘记了"设置"

: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))
                 {                         {

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

通过使用特定于语言的外部工具作为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