2

刚刚完成将PHP_Beautifier合并到 Vim 中,它删除空格的事实让我很恼火。显然这是自 2007 年以来的一个错误。有一个hack可以解决这个问题,但它会导致其他问题。相反,我决定使用轮回方法。

首先通过这里建议的命令将多个空行转换为单个空行

:g/^\_$\n\_^$/d

Next 将所有空白行转换为独特的东西(确保在美化过程中不会改变)

:%s/^[\ \t]*\n/$x = 'It puts the lotion on the skin';\r/ge

接下来像这样调用 PHP_Beautifier

:% ! php_beautifier --filters "ArrayNested() IndentStyles(style=k&r) NewLines(before=if:switch:foreach:else:T_CLASS,after=T_COMMENT:function)"<CR>

最后将所有唯一行更改回空行,如下所示

:%s/$x='It puts the lotion on the skin';//ge

当我独立测试它们时,所有四个都有效。我也有像这样映射到我的 F8 键的第三步

map <F8> :% ! php_beautifier --filters "ArrayNested() IndentStyles(style=k&r) NewLines(before=if:switch:foreach:else:T_CLASS,after=T_COMMENT:function)"<CR>

但是当我尝试通过管道符号将命令串在一起时,就像这样(我用空格填充管道以更好地显示不同的命令)

map <F8> :g/^\_$\n\_^$/d    |    %s/^[\ \t]*\n/$x = 'It puts the lotion on the skin';\r/ge      |      % ! php_beautifier --filters "ArrayNested() IndentStyles(style=k&r) NewLines(before=if:switch:foreach:else:T_CLASS,after=T_COMMENT:function)"     |     %s/$x = 'It puts the lotion on the skin';//ge<CR>

我收到以下错误

处理 /home/xxx/.vimrc 时检测到错误:
第 105 行:
E749: 空缓冲区

E482: 无法创建文件 /tmp/vZ6LPjd/0
按 ENTER 或键入命令继续

如何将这些多个命令绑定到一个键,在本例中为 F8。


感谢ib的回答,我终于让它工作了。如果有人遇到同样的问题,只需将此脚本复制到您的 .vimrc 文件中

func! ParsePHP()
    :exe 'g/^\_$\n\_^$/d' 
    :%s/^[\ \t]*\n/$x = 'It puts the lotion on the skin';\r/ge
    :exe '%!php_beautifier --filters "ArrayNested() IndentStyles(style=k&r)"'
    :%s/$x = 'It puts the lotion on the skin';//ge 
endfunc

map <F8> :call ParsePHP()<CR>
4

1 回答 1

1

For some Ex commands, including :global and :!, a bar symbol (|) is interpreted as a part of a command's argument (see :help :bar for the full list). To chain two commands, the first of which allows a bar symbol in its arguments, use the :execute command.

:exe 'g/^\_$\n\_^$/d' |
\   %s/^[\ \t]*\n/$x = 'It puts the lotion on the skin';\r/ge |
\   exe '%!php_beautifier --filters "ArrayNested() IndentStyles(style=k&r) NewLines(before=if:switch:foreach:else:T_CLASS,after=T_COMMENT:function)"' |
\   %s/$x = 'It puts the lotion on the skin';//ge
于 2011-10-31T13:22:16.357 回答