用vim排列函数参数列表

时间:2022-09-11 19:17:26

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

当定义或调用具有足够参数的函数来跨越多行时,我希望vim将它们排成一行。例如,

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

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

这个想法是让argsN-1的'a'与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?

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

4 个解决方案

#1


11  

The previous poster had it, but forgot the set

之前的海报有它,但忘记了套装

:set cino=(0<Enter>

From :help cinoptions-values

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

#2


7  

I believe you have to issue the command:

我相信你必须发出命令:

:set cino=(0

This is when using cindent of course.

这当然是使用cindent。

edit: I missed "set"

编辑:我错过了“设置”

#3


2  

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.

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

#4


1  

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

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

:!/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:

如果您要经常运行此类命令,可以通过在.vimrc中添加以下内容来定义命令:

command -range=% Tidy <line1>,<line2>!/path/to/tidy -config /path/to/configfile

#1


11  

The previous poster had it, but forgot the set

之前的海报有它,但忘记了套装

:set cino=(0<Enter>

From :help cinoptions-values

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

#2


7  

I believe you have to issue the command:

我相信你必须发出命令:

:set cino=(0

This is when using cindent of course.

这当然是使用cindent。

edit: I missed "set"

编辑:我错过了“设置”

#3


2  

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.

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

#4


1  

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

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

:!/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:

如果您要经常运行此类命令,可以通过在.vimrc中添加以下内容来定义命令:

command -range=% Tidy <line1>,<line2>!/path/to/tidy -config /path/to/configfile