2011-10-31 24 views
2

刚刚完成将PHP_Beautifier合并到Vim中,并且删除空白字符使我感到厌烦。显然这是自2007年以来bug。有一个hack来解决这个问题,但它会导致其他问题。相反,我决定使用一种方法。修改Vim中的PHP_Beautifier以不去掉空行

首先通过命令转换多个空行一个空行的建议here

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

下一页转换所有空行独特的,像这样的东西(确保它不会美化过程中改变)

:%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> 

我收到以下错误

 
Error detected while processing /home/xxx/.vimrc: 
line 105: 
E749: empty buffer 

E482: Can't create file /tmp/vZ6LPjd/0 
Press ENTER or type command to continue 

我怎么这些多个命令绑定到一个关键,在这种情况下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> 

回答

1

对于一些Ex命令,包括:global:!,酒吧符号(|)是 解释为命令的参数的一部分(有关完整的 列表,请参阅:help :bar)。要链接两条命令,其中的第一条命令允许其参数 中的条形符号使用:execute命令。

: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 
+0

有一个在你的榜样语法错误(没有匹配'“'的'” ArrayNested()'),和我有一个很难试图使其工作 – puk

+0

问题与映射做的关键。当我复制粘贴上面的代码到vim中(并添加''')它会起作用 – puk

+0

另外,如果没有发现任何模式,g/^ \ _ $ \ n \ _^$/d''会抱怨\ – puk