2011-10-31 25 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