Run PHP file from within vim - php

Is it possibly to run a PHP file from within vim? What im trying to do here is having a shortcut so whenever I need to run the file I'm editing to skip exiting vim and calling the PHP interpreter manually

Yes! It's possible to do what you want. Both running PHP from within vim, and creating a shortcut.
Matthew Weier O'Phinney writes:
Probably the most useful thing I've
done as a PHP developer is to add
mappings to run the current file
through (a) the PHP interpreter (using
Ctrl-M), and (b) the PHP interpreter's
linter (using Ctrl-L).
Vim Productivity Tips for PHP Developers
Example:
:autocmd FileType php noremap <C-M> :w!<CR>:!/usr/bin/php %<CR>
Or (this doesn't check the filetype beware)
:map <C-M> :w!<CR>:!/usr/bin/php %<CR>
Joe 'Zonker' Brockmeier writes:
Vim also allows you to execute a
command directly from the editor,
without needing to drop to a shell, by
using bang (!) followed by the command
to be run. For instance, if you're
editing a file in Vim and want to find
out how many words are in the file,
run
:! wc %
Vim tips: Working with external commands

You can use:
:!php %
% stands for the current document, and :! executes any shell command.
You can also create a shortcut for it.

I have this in my .vimrc
" set make command when editing php files
set makeprg=php\ -l\ %
set errorformat=%m\ in\ %f\ on\ line\ %l
and then I map to F7 (or whatever you want) with:
:map <F7> :make <CR>

Related

how to run PHP scripts live in Vim [duplicate]

Is it possibly to run a PHP file from within vim? What im trying to do here is having a shortcut so whenever I need to run the file I'm editing to skip exiting vim and calling the PHP interpreter manually
Yes! It's possible to do what you want. Both running PHP from within vim, and creating a shortcut.
Matthew Weier O'Phinney writes:
Probably the most useful thing I've
done as a PHP developer is to add
mappings to run the current file
through (a) the PHP interpreter (using
Ctrl-M), and (b) the PHP interpreter's
linter (using Ctrl-L).
Vim Productivity Tips for PHP Developers
Example:
:autocmd FileType php noremap <C-M> :w!<CR>:!/usr/bin/php %<CR>
Or (this doesn't check the filetype beware)
:map <C-M> :w!<CR>:!/usr/bin/php %<CR>
Joe 'Zonker' Brockmeier writes:
Vim also allows you to execute a
command directly from the editor,
without needing to drop to a shell, by
using bang (!) followed by the command
to be run. For instance, if you're
editing a file in Vim and want to find
out how many words are in the file,
run
:! wc %
Vim tips: Working with external commands
You can use:
:!php %
% stands for the current document, and :! executes any shell command.
You can also create a shortcut for it.
I have this in my .vimrc
" set make command when editing php files
set makeprg=php\ -l\ %
set errorformat=%m\ in\ %f\ on\ line\ %l
and then I map to F7 (or whatever you want) with:
:map <F7> :make <CR>

Aliasing a PHP CLI script so that the execution doesn't need PHP prefix

I have a PHP CLI script that I invoke using
php application.php --args etc
However I would like to alias the script so that I can just execute the script without prefixing the command line call with php and having the '.php' extension.
application --args etc
Is this possible? I pressume it is but lack the knowledge or probably the correct terms to search for in Google.
You need to do the thing that Mike Brants says add the next line to your sample.php file
#!/path/to/cli/php
but also you have to do these in linux
chmod +x sample.php
To tell the linux (unix) machine to interprete these file as an excecutable
You could just use a shebang to define the application to use for execution from within the file. So at the beginning of your script you would place something like this:
#!/path/to/cli/php
<?php
// start your PHP here
When executed from command line the OS will know to use the specified PHP CLI application to execute the script. Obviously the path to the PHP CLI excutable will vary based on your system and should be substituted with what I have shown above.
This is more flexible that aliasing IMO, as you don't need to enter an alias for each PHP script you may want to run in such a manner from the command line.
ahah. alias can be added to the .base_profile
http://www.hypexr.org/bash_tutorial.php#alias
Use a so called 'shebang':
In the first line of your script add:
#!/usr/bin/php
where /usr/bin/php is the path to your php cli executable.
That's it !
Even more logical and pleasant (at least my favorite) call is #!/usr/bin/env php
Quote part from the user contributed note on PHP manual it self:
uses "env" to find where PHP is installed: it might be elsewhere in the $PATH, such as /usr/local/bin.

Run PHP-GTK Programs without Command Prompt

Suppose I have made a project on PHP-GTK. Now how can I run the PHP-GTK without opening the command prompt?
Like in other software, we have a icon on the desktop, by click on that we can run the program, I want to do the same with my program made in PHP-GTK.
You can make a .bat file which executes with a double click. If you are under windows, I guess there must be something similar for unix. This is how you do it for windows:
"%CD%\..\..\PhpGtkRuntime\php.exe" "%CD%\test.php"
Of course you will adjust it to your folder depth but the idea is the same.
You can also look this answer which is closely realated and talks about things of immediate interest: Check here
this solution workes for me on windows XP SP3
let the phpGtk binary be C:\monitor\bin\php\php and the phpGtk main script file to be C:\monitor\system\monitor ui\monitor.php
use a bach script (monitor.bat) file like this:
#echo off
rem Hideself
"C:\monitor\bin\php\php" "C:\monitor\system\monitor ui\monitor.php" %*
rem Hideself
pause
rem is for comments in commands line,
the %* part is to pass any arguments provided to the phpGtk script
use this bat to exe converter to convert Bash .bat file to an executable .exe
rem Hideself and rem Showself are advanced commands and
they are just what we need
with this program you can:
- set an icon,
- include files
- set a password for the executable
Hint: you can phpGtk binarys but you should use %MyFiles% to refer to them
Advice: when your project is all done you can trim the pause command (it is just to see the errors php cli logged to you)
Caution: using multiple icons needs .NET framework
Hint: you can use Ressource Hacker to replace the single icon with multiple ones
Hint: you can use the WinRAR's SFX feature to handle the installation for you
hope this help somebody's life ! :)
if you use Linux, you can create a shell script and put the command with & to run in the background?
[command] &
If you use Windows, try
start [command]

Validate PHP syntax in VIM

I would like to know how if it's possible to validate if a PHP file is valid in VIM without closing VIM every time?
Thank you
You can execute shell commands in vim. This is the same as calling php -l filename.php from the shell:
:!php -l %
I have this mapped into my ~/.vim/after/ftplugin/php.vim file so that I only have to press F5:
map <F5> :!php -l %<CR>
Use :make with the following php specific settings:
:set makeprg=php\ -l\ %
:set errorformat=%m\ in\ %f\ on\ line\ %l,%-GErrors\ parsing\ %f,%-G
Your syntax errors will be in the Quickfix window. You can open this buffer with :copen or :cope for short. If you only want to open the window only if their are errors use :cwindow.
You can use :cnext and :cprev to move through the quickfix list to jump to the corresponding errors. I suggest Tim Pope's excellent unimpared.vim plugin to make moving through the list as simple as [q and ]q.
To simplify the workflow I suggest a mapping like this one:
nnoremap <f5> :update<bar>make<bar>cwindow<cr>
Now you can just hit <f5> and the buffer will be updated (if necessary), linted, and any errors will appear in the quickfix window.
To make this a bit more robust, add these commands to ~/.vim/after/ftplugin/php.vim. Example ~/.vim/after/ftplugin/php.vim
setlocal makeprg=php\ -l\ %
setlocal errorformat=%m\ in\ %f\ on\ line\ %l,%-GErrors\ parsing\ %f,%-G
nnoremap <buffer> <silent> <f5> :update<bar>sil! make<bar>cwindow<cr>
For more information:
:h quickfix
:h makeprg
:h errorformat
:w !php -l
The real credit goes to Can I see changes before I save my file in Vim? for the idea so up vote there.
But to explain on this post (mostly taken from above):
The above command works as follows:
The syntax for saving a file in vim is:
:w <filename>
The syntax for executing a shell command in vim is:
:!<command>
Executing the save command without a filename but rather a shell command behind it causes vim to write the files content to stdin of the shell instead of saving it in a physical file. You can verify this by executing
:w !cat
This should always print the files current content (which would have been written to a file instead).
4 You can check code with php -l from STDIN by piping it in
The file is "saved" to stdin, php lint is run with the stdin as input.
To check PHP syntax without having to save first you can use:
map :w !php -l
http://vim.wikia.com/wiki/Runtime_syntax_check_for_php
You should try a plugin like Neomake. It will show you in the gutter error markers for every PHP syntax errors. Go on the line to see the error itself. You can as well link it to PHP Mess detector and PHP Stan to show you errors and possible improvements in your code.
This combo is very powerful!
In case you are interested I wrote an article how to make a Vim PHP IDE. This is basically a list of essential plugins you should try out! Of course Neomake is part of it.
For just syntax highlight (what sometimes gives clues about errors) a non yet saved file (usually black and white on vim) just source the syntax file:
:source $VIMRUNTIME/syntax/[the syntaxfile].vim
Examples
:source $VIMRUNTIME/syntax/sh.vim
:source $VIMRUNTIME/syntax/php.vim
:source $VIMRUNTIME/syntax/javascript.vim
You can manually run the PHP linting commands, but if you're doing full time development in PHP then it's easier to use a generic syntax checking plugin.
Syntastic (as recommended by #lucapette) has long been the main syntax plugin for Vim with fairly relaxed requirements:
Syntastic itself has rather relaxed requirements: it doesn't have any external dependencies, and it needs a version of Vim compiled with a few common features: autocmd, eval, file_in_path, modify_fname, quickfix, reltime, statusline, and user_commands. Not all possible combinations of features that include the ones above make equal sense on all operating systems, but Vim version 7 or later with the "normal", "big", or "huge" feature sets should be fine.
Since Vim 8, which allows for asynchronous syntax checking, there are now two good plugins. These work by continuously linting your code so you don't have to save your code for errors to show up:
Asyncronous Lint Engine (ale)
Neomake (as recommended by #matthieu)
All of these should automatically call php -l for the current file as long as the syntax for that file is set to PHP (:set syntax=php if Vim doesn't automatically recognise the syntax).

Unix or VIM command to format PHP code

I know that you can use the indent command to beautify c code. I want to do the same thing with php but the command is giving errors (it is intended for c only I believe) is there a standard way to formatt php code with a command? I would be open to either a unix command that comes with OS X or a VIM command.
If vim has been set up to recognize PHP, type =G at the top of your file and it will reindent your code to the end.
To expand on rkulla's comment, = is the indentation command, while G is the command to go to the end of the document and gg goes to the beginning. If you want to indent the next 5 lines beginning from your current line, you can type =5= or 5==.
If you are using VIM, erjiang's answer is the best method. For reference for yourself or anyone reading this who does not use VIM, there is a PEAR module for beautifying PHP called 'PHP Beautifier'.
http://beautifyphp.sourceforge.net
See command line usage in the documentation.

Categories