A few Vim Tricks

I’m a fairly recent convert to using Vim for development, having started using it last May. Having become somewhat proficient (or so I think) I wouldn’t swap it for any other editor. I recently discovered a few config settings that made my life as a developer a little easier and thought I’d share them here. In this post we’ll find out how to configure Vim so that the gf (Goto File) command works with PHP namespaced classes. Along the way we’ll also enable the :find, :tabfind and :splitfind commands so that you can quickly search for any files in your project.

Find your Path…

The first thing that we need to configure is the path settings for your project. Vim uses the path setting to know where to look for files in projects and setting it correctly can save you from having to remember long file names. I use the localvimrc¬†plugin to configure settings for different projects that I’m working on and I add the path info to my .lvimrc file. Whether you use this plugin or just a global .vimrc file you simply need to add a setting line for each directory path that you would like Vim to include in searches. For example, I’m currently doing a lot of work with Symfony2. To add my src and vendor directories to my Vim path I simply need to add the following two lines:

set path+=/path/to/project/src/**
set path+=/path/to/project/vendor/**

You can of course add as many directories as you like. With that setup to search for a file you can use the :find command or any of it’s siblings. For example to find the file fooBar.php simply type “:find foo” and hit tab. Vim will offer you a choice of all files in your path that match what you’ve typed. For more info type “:h find”.

Getting gf to work with PHP

Vim’s gf command opens the file name that is under the cursor to edit. This works well ‘out of the box’ with files included or required in PHP but if you’re doing a lot of work with PHP frameworks you probably don’t have too many calls to include or require in your code. What you probably will have though is a lot of namespace declarations and use statements in your code. With a little tweaking you can make the gf command work with the namespaced classes though. Add the following two lines to your .vimrc file:

The first line replaces ‘\’ with ‘/’ whenever the gf command is using, assuming that your namespaces are PSR-0 compliant. The second makes sure that .php files are added to the list of file suffixes. With these two lines in your .vimrc, and assuming you’ve setup your path correctly, placing the cursor in a classname of a fully namespaced class and typing gf should result in the file with that class definition being opened. If you place the cursor in any other part of the namespace name and type gf you should find the directory that maps to that part of the namespace being opened in the netrw plugin. Two quick and easy adjustments to Vim config but that make day to day work much easier.

2 thoughts on “A few Vim Tricks

  1. Hello Jeremy,

    This blog post is more than 1 year old but let me ask one question. In .vimrc I have the following lines:
    set path=**
    set suffixesadd=.php
    set wildmode=longest,list
    set wildmenu
    set wildignore+=tmp\*,*.swp,*.zip,*.exe,*\.settings\*,\.classpath,\.project,*.class,\.springBeans,bin\*,target\*,*.jar,*\.git\*

    My project has 4153 php files. When I type command :find Request and hit key it takes few seconds to get list of found classes which have Request in their name. Do you remember if you also had to wait few seconds before results of find command were presented?

    Thanks,

    Marcin

    1. Hi Marcin,

      I don’t remember ever having to wait when using this command but with 4153 php files it doesn’t sound unreasonable… It’s difficult to be any more specific though.

      Hope that helps and sorry not to be able to offer any more help.

Leave a Reply