Tip 598 Printable Monobook Previous Next

created 2003 · complexity basic · author Macel Preda · version 6.0

While editing a .php file, you may need to view the php manual to see the parameters for a function.

You might open a browser and visit http://ro.php.net/FUNCTION_NAME (for example http://ro.php.net/printf).

However, you can use:

set keywordprg=a_script

Then, by default, when the cursor is on a WORD and you press K in normal mode, Vim will run man WORD. If you set keywordprg=a_script, Vim will run 'a_script WORD'.

So I have

set keywordprg=/home/marcel/php_doc

and php_doc looks like:

links http://www.php.net/$1

And now I have online help.

links is a text browser for Unix. Of course you can use lynx instead.

References[edit | edit source]

Comments[edit | edit source]

How can I get back in my file after I visit the doc file?

Press q to quit the browser, then press Enter at the Vim prompt: 'Hit ENTER or type command to continue'.

If you work in X and you use more mozilla than lynx i think you could like this script instead of other:

if ( $mozilla -remote "ping()" &> /dev/null );then
  $mozilla -remote "openurl(http://www.php.net/$1, new-tab)" &
  $mozilla http://www.php.net/$1 &

This script start mozilla if not running, and send it the new url. If you want to know the others command to use with -remote flag see here: http://www.mozilla.org/unix/remote.html

My script for opera:

/usr/bin/opera -windowname vimphphelp -newpage http://php3.de/$1

It will open all PHP documenation pages in its own window, with a new tab for each function.

php3.de works fast enough for me, change it to the closest mirror to you!

You can use the offline version of the php documentation too:

  • Put "set keywordprg=~/.vim/php_doc.sh" in ~/.vim/ftplugin/php.vim


FUNCTION=`echo $1 | sed 's/_/-/g'`
lynx file:///home/karel/dox/manuals/phpmanual/function.$FUNCTION.html

In the php_doc script, you can use this in Mac OS X to open the keyword in your default browser.

open http://www.php.net/$1

Here's my keywordprg script.

FUNCTION=`echo $1 | sed 's/_/-/g'`
links -dump http://uk.php.net/manual/en/print/function.$FUNCTION.php | sed -n -e '15p' -e '19,20p'

Here's my version: This one strips the top off section of weboutput so that the help page starts with the function definition.

#links http://www.php.net/$1
FUNCTION=`echo $1 | sed 's/_/-/g'`
links -dump http://uk.php.net/manual/en/print/function.$FUNCTION.php | tr -d '\r' | perl -ne "if (/---------------/) {\$f=1;} if (\$f==1) { print; }"

I created this file to return back just the description line.

FUNCTION=`echo $1 | sed 's/_/-/g'`
links -dump http://www.php.net/manual/en/print/function.$FUNCTION.php | grep -A 5 Description | grep $1

This will give the function reference up to the User Contributed Notes and will only alter keywordprg for php files:


FN=`echo $1 | sed 's/_/-/g'`
echo '**********************'
lynx -dump -nolist http://www.php.net/manual/en/print/function.$FN.php | sed -n /^$1/,/^.*User\ Contributed\ Notes/p | grep -v 'User\ Contributed\ Notes'


autocmd FileType php set keywordprg=~/.vim/php_doc

This is a tweak on earlier tweaks. Rather than running in the shell, the following vimscript function run the help in a split so it more similar to vim help (and easier to cut and paste from). It's also plain vimscript so all you need to do is put it in your vimrc

function! OpenPhpFunction (keyword)
  let proc_keyword = substitute(a:keyword , '_', '-', 'g')
  exe 'split'
  exe 'enew'
  exe "set buftype=nofile"
  exe 'silent r!lynx -dump -nolist http://www.php.net/manual/en/print/function.'.proc_keyword.'.php'
  exe 'norm gg'
  exe 'call search ("' . a:keyword .'")'
  exe 'norm dgg'
  exe 'call search("User Contributed Notes")'
  exe 'norm dGgg'
au FileType php map K :call OpenPhpFunction('<C-r><C-w>')<CR>

Couple of things. First, to just see the parameters list, as mentioned by original poster, you can use omnicomplete for php. This should do it:

:autocmd FileType php set omnifunc=phpcomplete#CompletePHP

The PHP manual has all kinds of good stuff in it, not just a list of functions. You can look up some of the SPL classes, for instance, or superglobals, by just surfing to php.net/foo.

This script is an enlargement and refinement of the one above. It grabs the php manual page using lynx and puts the results in the preview window, not just any old split, but a singleton. This makes it possible to call the manual ten times in a row without getting ten new split windows. You can also lookup classes/functions/constants in the retrieved manual page by calling the function with your cursor over the word of interest.

I remapped the shortcut key to control-p, so that lookups can be done while in insert mode.

The script, finally, also will return you back to the original cursor location after doing the lookup, even if you were in insert mode.

It looks long, but only because I've been commenting it as I figure out what each line does.

As with the previous script, this is vimscript, so plop it in your .vimrc and it should work. The only possible issue is whether you have lynx installed at the same location as below.

function! OpenPhpFunction (keyword)
  let proc_keyword = substitute(a:keyword , '_', '-', 'g')
  " create new preview split or switch to existing.
  "   vim has a Preview split which is a singleton,
  "   one per tab. This is perfect for repeated
  "   uses of the manual feature.
  " for some reason, I was getting errors
  " when trying to open a preview window.
  " but I'm unable to reproduce. Who knows...
  " try opening a new preview window.
    exe 'pedit'
    " sometimes seems to throw an error
  catch /.*/
  " switch to the preview
  exe 'wincmd P'
  " edit a new buffer
  exe 'enew'

  " don't associate any file or swap file
  " with this buffer. http://vim.wikia.com/wiki/VimTip135
  " the first command automatically names
  " the buffer [Scratch]. We could use this
  " to reuse the scratch window.
  exe "set buftype=nofile"
  exe "setlocal noswapfile"

  "call browser and fetch the file. we use lynx here.
  "php actually has a great script that fetches
  "info on lots of things other than functions.
  "so let it do its thing.
  exe 'silent r!lynx -dump -nolist http://php.net/'.proc_keyword

  " now we format the results:
  " enter normal mode and go to top of manual entry
  exe 'norm gg'
  " 1. this was given by original author
  "    at http://vim.wikia.com/wiki/PHP_online_help
  "    Problem: doesn't search far enough down the page
  "    exe 'call search ("' . a:keyword .'")'
  " 2. I came up with this to remove stuff at top of
  "    file for function retrievals.
  "    Idea: search down to Description. Then go up 8 lines.
  "    Problem: doesn't work for non-function man pages.
  "    exe 'call search("Description")'
  "    exe 'norm 8kdgg'
  " 3. Best idea so far: search down to a really long underscore.
  "    This will be the search box if your underscore is short.
  "    If it's long enough, you'll match on the horizontal line
  "    that is above the definition we really want.
  exe 'call search("____________________________________")'
  exe 'norm dgg'
  " delete the user notes at the bottom. it's a lot of text,
  " and they are almost completely useless.
  exe 'call search("User Contributed Notes")'
  exe 'norm dGgg'

" manual lookup is mapped to ctrl-p
" ctrl-o is used to jump out of insert mode for one command.
" - jump out for one command <C-O>
" - call OpenPhpFunction with the word under cursor
" - CR seems to indicate end of fcn call
" - <C-O> again - we are in insert mode in the man page
" - this time, we jump back to previous window.
" - at the end of the day, we are still in insert mode,
"   the cursor is in exactly the same spot, and the man
"   page for php is visible
inoremap <C-p> <C-O>:call OpenPhpFunction('<c-r><c-w>')<CR><C-O>:wincmd p<CR>
nnoremap <C-p> :call OpenPhpFunction('<c-r><c-w>')<CR>:wincmd p<CR>
vnoremap <C-p> :call OpenPhpFunction('<c-r><c-w>')<CR>:wincmd p<CR>
Community content is available under CC-BY-SA unless otherwise noted.