在Vim中修改PHP_Beautifier以不去除空行
刚刚完成将 PHP_Beautifier 合并到 Vim 中,它删除空格的事实让我很恼火。显然这是自 2007 年以来的一个 bug。有一个 hack 来解决此问题,但它会导致其他问题。相反,我决定使用迂回方法。
首先按照此处建议的命令将多个空行转换为单个空行
:g/^\_$\n\_^$/d
Next 将所有空行转换为唯一的行,如下所示(确保它在美化过程中不会更改)
:%s/^[\ \t]*\n/$x = 'It puts the lotion on the skin';\r/ge
Next 像这样调用 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>
Just finished incorporating PHP_Beautifier into Vim and the fact that it removes whitespace irks me. Apparently it's a bug since 2007. There is a hack to fix this problem, but it leads to other problems. Instead I decided to use a round about method.
First Convert multiple blank lines to a single blank line via the command as suggested here
:g/^\_$\n\_^$/d
Next Convert all blank lines to something unique like so (make sure it does not get changed during beautification)
:%s/^[\ \t]*\n/$x = 'It puts the lotion on the skin';\r/ge
Next Call PHP_Beautifier like so
:% ! php_beautifier --filters "ArrayNested() IndentStyles(style=k&r) NewLines(before=if:switch:foreach:else:T_CLASS,after=T_COMMENT:function)"<CR>
Finally Change all unique lines back to empty lines like so
:%s/$x='It puts the lotion on the skin';//ge
All four work when I tested them independently. I also have the third step mapped to my F8 key like so
map <F8> :% ! php_beautifier --filters "ArrayNested() IndentStyles(style=k&r) NewLines(before=if:switch:foreach:else:T_CLASS,after=T_COMMENT:function)"<CR>
But when I try to string the commands together via the pipe symbol, like so (I padded the pipes with whitespace to better show the different commands)
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>
I get the following error
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
How do I bind these multiple commands to a key, in this case F8.
Thanks to ib's answer, I finally got this to work. If anyone is having this same problem, just copy this script into your .vimrc file
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于某些 Ex 命令,包括
:global
和:!
,条形符号 (|
) 是解释为命令参数的一部分(请参阅
:help :bar
了解完整信息列表)。要链接两个命令,第一个命令允许在其中包含条形符号
参数,使用
:execute
命令。For some Ex commands, including
:global
and:!
, a bar symbol (|
) isinterpreted as a part of a command's argument (see
:help :bar
for the fulllist). To chain two commands, the first of which allows a bar symbol in its
arguments, use the
:execute
command.