在Vim中修改PHP_Beautifier以不去除空行

发布于 2024-12-13 01:26:05 字数 2255 浏览 1 评论 0原文

刚刚完成将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2024-12-20 01:26:05

对于某些 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文