Run PHP Script in Vim

Vim IconWhen developing PHP apps I often want to run the current file and see the results. Sometimes I even want to save those results, or perhaps manipulate the results. So I wrote this little vim function to run my currently open PHP file and place the results in a new VIm buffer.

function! RunPHP ()
  let filename = expand("%:p")
  let root = getcwd()
  exe 'cd %:p:h'
  exe 'enew'
  exe 'set buftype=nofile'
  exe 'setlocal noswapfile'
  exe 'r!/Applications/MAMP/bin/php/php5.3.13/bin/php ' . filename
  exe 'norm gg'
  exe 'cd ' . root
endfunction

I also have some additional settings configured run add auto completion for PHP functions, check PHP syntax, and run the PHP script.

" PHP auto complete
autocmd FileType php set omnifunc=phpcomplete#CompletePHP

" Check PHP syntax
map  :!/Applications/MAMP/bin/php/php5.3.13/bin/php -l %

" Run current PHP file and place output in new buffer
map  :call RunPHP()

3 thoughts on “Run PHP Script in Vim

  1. Nick Lo says:

    Is there much advantage that your method offers over just running the current buffer via the command line php command?:

    :!php %

    • Nasir says:

      Is there any advantage in using your method………?

      I am a newbie and want to a run a PHP script .. without checking the outputs in a browser i want to see its output in vim , to speed up my learning process

      Thanks

Leave a Reply

Your email address will not be published.