History of Vim Tricks

Version 22

Vim Tricks

Vim and PHP for coding utopia including advanced techniques such as cscope

Created by: xing, Last modification: 25 Jul 2008 (18:25 UTC) by xing

vimrc tips

  • if you put these 2 lines in your vimrc, you can use :make to check your php code for typos:
    set makeprg=php\ -l\ %
    set errorformat=%m\ in\ %f\ on\ line\ %l


vim + cscope is insanely cool.

This shell script will set up your machine with the stuff described below. all you need to do is download it, set the file executable and run it.

curl http://www.bitweaver.org/liberty/download/file/884 -o vim-cscope
chmod +x vim-cscope

What's cscope?

cscope will generate a list of functions used in bitweaver and you can use a few keymaps to very quickly jump to those functions from within bitweaver (or any source code tree). cscope is faster and more feature rich than ctags.

Setting it up

  1. Install cscope (via rpm, etc)
  2. Add this file to /usr/local/bin/cscope-php


    for arg in "$@"; do
        if [ -d $arg ]; then
            #echo Create list of php files in cscope.files
            find $arg \( -name "*.php" -or -name "*.tpl" \) -and -not -regex "./temp/.*" -and -not -regex ".*/language.php" -print > $arg/cscope.files
            #echo Create cscope database in cscope.out
            cscope -b -i $arg/cscope.files -f$arg/cscope.out
            rm -f $arg/cscope.files
            echo "Directory not found: $arg"
  3. Have a periodic crontab like:


    0 * * * * /usr/local/bin/cscope-php /path/to/project1 /other/path/to/project2
  4. get vim plugin

    mkdir -p ~/.vim/plugin/ && curl http://cscope.sourceforge.net/cscope_maps.vim | sed 's/\bcs add\b/" cs add/' > ~/.vim/plugin/cscope_maps.vim
  5. Add the following to your ~/.vimrc (or /etc/vimrc for cool people)

    ~/.vimrc or /etc/vimrc

    " Some scsope specific settings
    if has("cscope")
        " Some scsope specific settings
        set cscopequickfix=s-,c-,d-,i-,t-,e-

        " always use cscope instead of tags
        set cst

        " path to cscope
        set csprg=/usr/bin/cscope

        " search databased before searching tag files
        set cscopetagorder=0

        " don\'t display cscope messages when adding database
        set nocsverb

        " add any database in current directory
        if filereadable("cscope.out")
            cs add cscope.out
            " else search up a few directories
        elseif filereadable("../cscope.out")
            cs add ../cscope.out
        elseif filereadable("../../cscope.out")
            cs add ../../cscope.out
        elseif filereadable("../../../cscope.out")
            cs add ../../../cscope.out
        elseif filereadable("../../../../cscope.out")
            cs add ../../../../cscope.out
        elseif filereadable("../../../../../cscope.out")
            cs add ../../../../../cscope.out
            " else add database pointed to by environment
        elseif \$CSCOPE_DB != ""
            cs add \$CSCOPE_DB

        " reset cscope verbosity
        set csverb

        " easily reload cscope database
        nmap <F11> :cs reset<cr>

    If you get an error message about a duplicate cscope database has been added, go to your ~/.vim/plugin/cscope_maps.vim file and comment out the section where it says
     41:     if filereadable("cscope.out")
     42:         cs add cscope.out
     43:     " else add the database pointed to by environment variable
     44:     elseif $CSCOPE_DB != ""
     45:         cs add $CSCOPE_DB
     46:     endif
  6. To test if this worked:
    $ cd /path/to/bitweaver
    $ vim wiki/index.php +/verifyPackage
      move your cursor over verifyPackage
      <CTRL-\>g will go to where the function was defined
      <CTRL-\>s will show all places where the function has been used
      use ':copen' to view the complete list.

Using it all

Some funky tips on how to use your new setup (taken from cscope_maps.vim):

Mapping Overview

The following maps all invoke one of the following cscope search types:

  's'   symbol:   find all references to the token under cursor
  'g'   global:   find global definition(s) of the token under cursor
  'c'   calls:    find all calls to the function name under cursor
  't'   text:     find all instances of the text under cursor
  'e'   egrep:    egrep search for the word under cursor
  'f'   file:     open the filename under cursor
  'i'   includes: find files that include the filename under cursor
  'd'   called:   find functions that function under cursor calls

To do the first type of search, hit 'CTRL-\', followed by one of the
cscope search types above (s,g,c,t,e,f,i,d).  The result of your cscope
search will be displayed in the current window.  You can use CTRL-T to
go back to where you were before the search.

Using 'CTRL-spacebar' (intepreted as CTRL-@ by vim) then a search type
makes the vim window split horizontally, with search result displayed in
the new window.

Hitting CTRL-space *twice* before the search type does a vertical
split instead of a horizontal one (vim 6 and up only)

As you can see above, there's many new ways to now navigate your project. Very cool stuff happens when you move the curson over a function like verifyPackage in wiki/edit.php and then do:
  1. vim wiki/edit.php +/verifyPackage (open the file on the first occurrance of verifyPackage)
  2. n (to move the cursor to verifyPackage)
  3. <ctrl->s (this will search the database for all occurances of verifyPackage)
  4. :copen (will open a window with a list of all occurrances of verifyPackage)
  5. navigate to the occurrance you're interested in and hit <enter>
  6. <ctrl-o> (move backward in your jump history - might need to hit this more than once to return to edit/wiki.php)
  7. <ctrl-i> (move forward in your jump history)</ctrl-i></ctrl-o></enter></ctrl->
Page History
25 Jul 2008 (18:26 UTC)
Current • Source
View • Compare • Difference • Source
View • Compare • Difference • Source
View • Compare • Difference • Source
View • Compare • Difference • Source
View • Compare • Difference • Source
View • Compare • Difference • Source
View • Compare • Difference • Source
View • Compare • Difference • Source
View • Compare • Difference • Source
View • Compare • Difference • Source
View • Compare • Difference • Source
View • Compare • Difference • Source