1

I am trying to write simple function which runs a shell command and prints the result into vims shell output window, this here is a basic version of the function which does work:
function defined in ~/.vimrc

function! Runphp()
    let s:runphp='cat /tmp/php_snippet | php'
    echo(system(s:runphp))
endfunction

file /tmp/php_snippet

<?php
$a = ['apple', 'orange', 'bannana', 'pear'];
print_r($a);

If I run :call Runphp() vim prints the result of the executed PHP in the result window, all good.
The problem occurs where the <?php tag doesn't already exist in /tmp/php_snippet i.e.

file /tmp/php_snippet looks like this:

$a = ['apple', 'orange', 'bannana', 'pear'];
print_r($a);

and I try to inject <?php as part of the shell command, i.e.

function! Runphp()
    let s:runphp='{ echo "<?php"; cat /tmp/php_snippet } | php'
    echo(system(s:runphp))
endfunction

If I :call Runphp() now, vim returns

Error detected while processing function Runphp:
line 3:
E484: Can't open file /tmp/vReTqKG/9

How can I deal with this error so I can get this extra <?php injected into the shell command?

  • 2
    Brace-enclosed compound commands are supposed to end with either a newline or a semicolon before the closing brace. – muru May 15 '16 at 23:57
  • 1
    @muru nice spotting! your knowledge always impresses me. thanks – the_velour_fog May 16 '16 at 0:11
2

From man bash:

Compound Commands
   A compound command is one of the following:
   ...
   { list; }
          list  is simply executed in the current shell environment.  list
          must be terminated with a newline or semicolon.  This  is  known
          as  a  group  command. 

Note: in { echo "<?php"; cat /tmp/php_snippet } | php, you need a ; before the closing brace. Unless you're using zsh, in which case the whole thing would have worked fine.

The output from bash isn't particularly helpful:

$ bash -c '{ echo "<?php"; cat /tmp/php_snippet } | php'  
bash: -c: line 1: syntax error: unexpected end of file

However, I can find no way to get system to return even this error message, without manually calling bash -c.

  • Yes it would be interesting if vim use reliably use zsh. I'm assuming - but could be wrong - that the shell vim uses for system() is based on what you have set as your default shell for your UNIX system account. When I have set zsh as default, I have experienced errors in vimdiff so I keep the system shell as bash - then run exec zsh at the end of .bashrc so vim continues to use bash and I get zsh at prompt. it takes a bit longer to start shell but its workable – the_velour_fog May 16 '16 at 23:01
  • Odd, I have never experienced any problems in Vimdiff (my shell is zsh across Ubuntu and Arch Linux systems). Probably a bug fixed in newer Vims. – muru May 16 '16 at 23:02
  • I used to get the errors on Ubuntu 14.04 when using vimdiff as git difftool. I am now running 16.04 with vim-gtk from pi-rho repo and I just tested changing the shell to zsh. It's now worse, errors in core vim and more errors in vimdiff. e.g. when running git difftool- "includes-shared/vim/user_setup_commands.sh" [readonly] 310L, 9093C/usr/bin/zsh: can't open input file: globstar /usr/bin/zsh: can't open input file: globstar E810: Cannot read or write temp files E97: Cannot create diffs – the_velour_fog May 16 '16 at 23:39
  • @the_velour_fog O.o no errors on 14.04 or 16.04. How did you set zsh as your shell? – muru May 16 '16 at 23:47
  • added set shell=/bin/bash into ~/.vimrc seems to have fixed it. so now zsh is the default vim uses bash. I got the idea after searching and finding this <coderwall.com/p/lzf7ig/using-vim-with-fish-shell> – the_velour_fog May 16 '16 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.