Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

118 lines (68 sloc) 3.449 kb


vim-php-namespace is a vim plugin for inserting "use" statements automatically.


Import classes (add use statements)

Automatically adds the corresponding use statement for the class under the cursor.

To use this feature, add the following mappings in ~/.vimrc:

inoremap <Leader>u <C-O>:call PhpInsertUse()<CR>
noremap <Leader>u :call PhpInsertUse()<CR>

Then, hitting \u in normal or insert mode will import the class under the cursor.

new Response<-- cursor here or on the name; hit \u now to insert the use statement

Make class names fully qualified

Expands the class name under the cursor to its fully qualified name.

To use this feature, add the following mappings in ~/.vimrc:

inoremap <Leader>e <C-O>:call PhpExpandClass()<CR>
noremap <Leader>e :call PhpExpandClass()<CR>

Then, hitting \e in normal or insert mode will expand the class name to a fully qualified name.

$this->getMock('RouterInterface<-- cursor here or on the name; hit \e now to expand the class name'


Using pathogen

git clone git:// ~/.vim/bundle/vim-php-namespace

Using vundle

Add to vimrc:

Bundle 'arnaud-lb/vim-php-namespace'

Run command in vim:


Manual installation

Download and copy plugin/phpns.vim to ~/.vim/plugin/

Post installation

Generate a tag file

The plugin makes use of tag files. If you don't already use a tag file you may create one with the following command; after having installed the ctags or ctags-exuberant package:

ctags-exuberant -R --PHP-kinds=+cf


ctags -R --PHP-kinds=+cf


ctags doesn't indexes traits by default, you have to add a --regex-php option to index them:

ctags -R --PHP-kinds=+cf --regex-php=/^[ \t]*trait[ \t]+([a-z0_9_]+)/\1/t,traits/i

Alternatively, create a ~/.ctags file with the following contents:

--regex-php=/^[ \t]*trait[ \t]+([a-z0_9_]+)/\1/t,traits/i

You could also use this patched version of ctags

Automatically updating tags

The AutoTags plugin can update the tags file every time a file is created or modified under vim.

To keep updates fast, AutoTags won't operate if the tags file exceeds 7MB. To avoid exceeding this limit on projects with many dependencies, use a separate tags file for dependencies:

# dependencies tags file (index only the vendor directory, and save tags in ./tags.vendors)
ctags -R --PHP-kinds=+cf -f tags.vendors vendor

# project tags file (index only src, and save tags in ./tags; AutoTags will update this one)
ctags -R --PHP-kinds=+cf src

Do not forget to load both files in vim:

" ~/.vimrc
set tags+=./tags.vendors,tags.vendors

Key mappings

See Features section for adding key mappings.

The <Leader> key usually is \.


This was originally based on a similar script for java packages found at (in comments).

Jump to Line
Something went wrong with that request. Please try again.