2

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>
Community
  • 1
  • 1
puk
  • 16,318
  • 29
  • 119
  • 199

1 Answers1

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
ib.
  • 27,830
  • 11
  • 80
  • 100
  • There's a syntax error in your example (no matching `"` for `"ArrayNested()`), and am having a hard time trying to make it work – puk Oct 31 '11 at 20:55
  • Problem has to do with mapping to the key. When I copy paste the above code into vim (and add `"`) it works. – puk Oct 31 '11 at 22:08
  • Also, `'g/^\_$\n\_^$/d'` complains if no patterns are found\ – puk Oct 31 '11 at 22:08
  • @puk: I just copied your commands for the matter of illustration of using `:exe` to chain `|`-sensitive commands. Debugging your original commands is a separate issue. – ib. Nov 01 '11 at 06:23
  • @puk: The `:global` command does not cause any errors if there is no matching lines. What kind of "complaint" do you get exactly? Does it occur when you manually run that `:global` command? – ib. Nov 01 '11 at 06:28
  • I actually fixed (if you read the modified section of my question). The problem was in the `map `. It didn't work, so I moved your answer into a function, and instead did `map :call ParsePHP()`. Thanks for your help. – puk Nov 01 '11 at 06:46