Vim for PHP: The Complete Guide for a Powerful PHP IDE
Tags: vim, php, productivity
Published: 2020-07-27 14:38:24

---
title: Vim for PHP: The Complete Guide for a Powerful PHP IDE
published: true
description: I can see you spitting your cereals in front of you computer. 'What? A PHP IDE with Vim? Are you insane?'. If I am, I'm not the only one: many developers use Vim, why not PHP developers? This article will describe everything you need for you to build your own personal IDE, which fits your way of working and answers your specific needs.
tags: #vim #PHP #productivity
cover_image: https://thevaluable.dev/images/2017/11/vim-love.jpg
canonical_url: https://thevaluable.dev/vim-php-ide/
---

---------

*I'm currently writing a [book to build your own Mouseless Development Environment](https://themouseless.dev/), where I explain how to combine powerful tools to improve and customize your development worklow.*

---------

I can see you spitting your cereals in front of you computer. "What? A PHP IDE with Vim? Are you insane?"

Am I? It's definitely possible to make a very powerful IDE with Vim: you need to know what plugins to install and what are their purposes. 

The list of plugins I propose here will cover most of your needs:  project management, autocompletion, PHPDoc generation, powerful search and replace tools, debugger... all these features will be at your fingertips, for free.

This configuration won't block Vim to work perfectly with other plugins for different languages as well.

Unlike many articles on the net, I tried to list and describe **all** the plugins which are necessary for a good PHP IDE.
Of course you need to read the documentation of each plugins to configure them as you wish. That's one of the multiple reason Vim is so great: you can make your own IDE which suits perfectly your needs and preferences.

If you need some concrete configuration examples you can have a look at [my general vim configuration](https://github.com/Phantas0s/.dotfiles/blob/master/nvim/init.vim).
You can as well look at [my configurations for some specific plugins](https://github.com/Phantas0s/.dotfiles/tree/master/nvim/pluggedconf).

Finally you can let a comment if you have difficulties configuring these plugins. I would be happy to help you as much as I can.

## Disclaimer

I use these plugins with Neovim on Arch Linux, which means that I have no clue if they work on MacOs or Windows. It should, but be aware I never tested it.

[Neovim](https://neovim.io/) and [Vim](https://vim.sourceforge.io/) are almost the same software in practice, that's why I refer to them in this article under the global name "Vim". However, keep in mind that the plugin I refer to are more likely to work with Neovim.

## PHP Vim Plugins

I try here to highlight every strength of every plugin I use. If you just want a plain boring list, you can directly go to the [plugin reference list](https://thevaluable.dev/vim-php-ide/#quick-reference) at the end of the article.

### Plugin Manager

First of all you need a plugin manager. This plugin is the mother-of-all-plugin: it will help you install new plugins, delete or update them via simple command lines. 

* [junegunn/vim-plug](https://github.com/junegunn/vim-plug)

vim-plug is all you need to manage your Vim's plugins.
You only need to install it and put this configuration on top of your [vimrc](http://vim.wikia.com/wiki/Open_vimrc_file):
```vim
call plug#begin('~/nvim/plugged')
Plug 'tpope/vim-fugitive'
call plug#end()
```
The line `Plug 'tpope/vim-fugitive'` is an example of plugin you can install. The keyword `Plug` need to be followed by any plugin's github repository.

You need to declare your plugins between the lines `call plug#begin` and `call plug#end`. 

Simple as that!

### Generating Ctags

This tag file is an [index](http://ctags.sourceforge.net/whatis.html) which can be set for a lot of OOP languages, including PHP.

Creating a tag file for each of your projects is recommended:

1. You will be able to jump to method and class definitions easily (using `ctrl + ]` by default)
2. Some plugins in this list need to have a tag file to work

There are two solutions in order to generating this file: Universal ctags **or** Gutentags. I personally prefer the first one but you can of course test both.

##### 1 - Universal ctags

For this first approach to generate a tag file you need to use git for your PHP projects (and I secretly hope you do!).

First you need to install [universal-ctags](https://github.com/universal-ctags/ctags.git) in order to generate this tag file.

**If it's not available in your package manager**, you will need to compile it.
Execute these command lines and you will download and compile [universal-ctags](https://github.com/universal-ctags/ctags.git) in the current directory. A little suggestion: it's good practice to keep all your compiled software at one place (I personally have them in the folder `~/bin`):

```
git clone git@github.com:universal-ctags/ctags.git
cd ctags
./autogen.sh
./configure
make
sudo make install
```

Then you need to add a git hook to automatically generate this tag file. Create a `ctags` file in the directory `.git/hooks/` of your PHP project with the following content:

```bash
#!/bin/sh
set -e
PATH="/usr/local/bin:$PATH"
dir="`git rev-parse --git-dir`"
trap 'rm -f "$dir/$$.tags"' EXIT
ctags --tag-relative=yes -R -f "$dir/$$.tags" --fields=+aimlS --languages=php --PHP-kinds=+cdfint-av --exclude=composer.phar --exclude=*Test.php --exclude=*phpunit* --exclude="\.git"
mv "$dir/$$.tags" "$dir/tags"
```

Now you need to change the permissions of the file:
`sudo chmod 777 ctags`

Finally you need to add the following line in your [vimrc](http://vim.wikia.com/wiki/Open_vimrc_file). It will generate the tags each time you save a PHP file.
`au BufWritePost *.php silent! !eval '[ -f ".git/hooks/ctags" ] && .git/hooks/ctags' &`

You can test if it works by opening any PHP file in your project and save it. Normally a `tags` file should appear in the `.git` directory of your project. This is what we need!

##### 2 - Gutentags

* [ludovicchabant/vim-gutentags](https://github.com/ludovicchabant/vim-gutentags)

This second approach is more straightforward: all you need is to install gutentags plugin, configure it as you wish and you are ready to go!

### Essentials plugins
The plugins of this section are simply the-best-and-must-have-plugins-of-the-death. They improve Vim... a lot! They are not specifically for PHP development but they will bring some great functionalities major IDEs have by default.

* [scrooloose/nerdtree](https://github.com/scrooloose/nerdtree)

Vim comes with a very simple file browser which is... not enough.
If you want a more sophisticated file tree in your editor, this one is very powerful.

* [bfredl/nvim-miniyank](https://github.com/bfredl/nvim-miniyank)

Miniyank is very good to navigate easily into [Vim's registers](http://www.brianstorti.com/vim-registers/). You can easily go through your registers by using whatever keystroke you want. Very simple and powerful when you know that you need something you copied or deleted some times ago.

* [moll/vim-bbye](https://github.com/moll/vim-bbye)

This plugin allow you to close a [buffer](http://vim.wikia.com/wiki/Vim_buffer_FAQ) without closing your window. I use it all the time, it's great not to mess up your window layout and keep the possibility to close files.

* [itchyny/lightline.vim](https://github.com/itchyny/lightline.vim)

lightline.vim is a good status bar to have current file and status information you need at all time. You don't remember in what mode you are in? What file you currently modify? What the encoding of the file? This status bar will give you the information you need.

* [tpope/vim-commentary](https://github.com/tpope/vim-commentary)

[Tim Pope](https://github.com/tpope) is a very prolific contributor in the Vim world. All his plugins are worth trying.
Vim-commentary will comment every line selected via a simple [keystroke](https://www.computerhope.com/jargon/k/keystroke.htm). It works perfectly with many languages (PHP, JavaScript, Go...).

* [tpope/vim-abolish](https://github.com/tpope/tpope-vim-abolish)

One of the Vim's feature I use the most is the [substitution](http://vim.wikia.com/wiki/Search_and_replace). It allows you to simply substitute one string with another in the current file.

This plugin makes the substitution functionality smarter. For  example it allows you to keep the plural of the words you substitute or the uppercase letters. It can even replace camelCase by snake_case by dot.case...

A very nice addition to Vim.

### Project management plugins

It is so natural nowadays to have disparate code files organized into a project in many IDE or editors. It feels so natural we don't thing how handy it is anymore. 

You can achieve the same thing in Vim by combining two plugins. 

* [amiorin/vim-project](https://github.com/amiorin/vim-project)

With vim-project you will be able to overwrite any vim config for one single project! Like many IDE, it allows you to have a totally personalized configuration depending on the project currently open.

This plugin provide as well a startup page to Vim and let you choose the project you want to open. Unfortunately, this startup page seems not to work on Neovim.

That's why I use vim-project with vim-startify.

* [mhinz/vim-startify](https://github.com/mhinz/vim-startify)

Do you know [Fortune](https://en.wikipedia.org/wiki/Fortune_%28Unix%29)? vim-startify will use it to display random development-related quotes each time you open Neovim. This may sound silly but it's great!

On this very same page startify will display as well every projects you have configured and let you choose the one you want to open.

Since I keep my project configuration files separated from my vimrc, here an example how you can configure a project with vim-project and vim-startify:

```vim
let g:startify_bookmarks = [ 
            \ {'1': '~/workspace/mySuperProject/README.md'},
            \]

call project#rc("~/workspace")
Project  'mySuperProject'
Callback 'mySuperProject', ['Symfony', 'mySuperProject']

function! mySuperProject(...)
    let g:vdebug_options["path_maps"] = {
    \       "/mySuperProject": "/home/mySuperUser/workspace/mySuperProject"
    \}
    PadawanStartServer
endfunction

function! Symfony(...)
    let g:ultisnips_php_scalar_types = 1

    " standard phpcs config
    let g:neomake_php_phpcs_args_standard = 'PSR2'

    " php cs fixer 
    let g:php_cs_fixer_php_path = "php"

    autocmd FileType php nnoremap <leader>g :silent :call PhpCsFixerFixFile()<CR>
endfunction

```

I defined some special configuration in the functions `Symfony` and `mySuperProject` I can then attach to the project `mySuperProject` via the Callback line.

This configuration use plugins I describe below.

### Syntax plugins

These plugins are very specific for PHP development.

* [StanAngeloff/php.vim](https://github.com/StanAngeloff/php.vim)

A simple plugin which will improve the syntax highlighting for PHP. Like a lot of plugins in this article, it is highly configurable.

* [stephpy/vim-php-cs-fixer](https://github.com/stephpy/vim-php-cs-fixer)

This one will automatically format your code whenever you want (via a keystroke or each time you save a PHP file for example). By default it will format your code following[PSR1 / PSR2](http://www.php-fig.org/psr/psr-2/) rules but you can as well configure your own.

### Autocompletion plugins

I was very afraid to quit IntelliJ (PhpStorm's indexation engine which allows a very nice autocompletion) when I began to fully use Vim. At the end I didn't miss it one second... thanks to the following plugins!

* [ncm2/ncm2](https://github.com/ncm2/ncm2)

An auto-completion engine is mandatory to trigger automatically the auto-completion itself. ncm2 is my favorite since it's easy to install, easy to configure and easy to use. I never had a problem with it.

However this is not enough. You will need these plugins to specifically autocomplete PHP code:

* [phpactor/phpactor](https://github.com/phpactor/phpactor)
* [phpactor/ncm2-phpactor](https://github.com/phpactor/ncm2-phpactor)

phpactor is simply a must have. It can help you for a lot of things (see the [Refactoring / code styling plugins](/vim-php-ide/#refactoring-plugins) section). The autocompletion it provides is very good and pretty stable.

It allows you as well to import automatically class namespaces or even to jump to classes or methods definition without ctags.

The plugin ncm2-phpactor is meant to link phpactor to ncm2.

### Searching / replacing plugins

What about search and replace in a file or even in an entire project? Can Vim do that as well?

Of course it can, and it does it very well. These plugins are way more faster and powerful than anything I've ever tested.

* [https://github.com/junegunn/fzf](https://github.com/junegunn/fzf)
* [junegunn/fzf.vim](https://github.com/junegunn/fzf.vim)

Simply put: fzf is the best fuzzy searcher you will ever find. Written in Go, it can search everything in any files at light speed. To be honest, my switch from PhpStorm to Vim was in part because of this plugin!
It is that good.

The first plugin of this list will install the fuzzy searcher itself: you can use it in the terminal for searching... whatever you want. Searching through your command line history (for example) is a major feature I couldn't live without. 

The second plugin will enable fzf in Vim. Then you can configure some keystroke to search through your file history, your buffer or to search a file in your project...

* [https://github.com/BurntSushi/ripgrep](https://github.com/BurntSushi/ripgrep)

Ripgrep is not a Vim plugin but can be combined with fzf to search occurrences into multiple files. Very powerful and again incredibly fast.

You need to install ripgrep on your machine (for those who use [Arch linux](https://www.archlinux.org/) you simply have to type `pacman -S ripgrep`) and write a bit of configuration in your vimrc. 

For example:

```vim
nnoremap <leader>a :Rg<space>
nnoremap <leader>A :exec "Rg ".expand("<cword>")<cr>

autocmd VimEnter * command! -nargs=* Rg
  \ call fzf#vim#grep(
  \   'rg --column --line-number --no-heading --fixed-strings --ignore-case --no-ignore --hidden --follow --glob "!.git/*" --color "always" '.shellescape(<q-args>), 1,
  \   <bang>0 ? fzf#vim#with_preview('up:60%')
  \           : fzf#vim#with_preview('right:50%:hidden', '?'),
  \   <bang>0)

```

When you enter the keystroke `<leader> a` and type something in the command line prompt, ripgrep will search this something through all the files of your project.

Then you can fine tune your search in the search window thanks to fzf.
You can even have a preview of the file highlighted in the search by typing `?`.

* [https://github.com/wincent/ferret](https://github.com/wincent/ferret)

Last but not least, here a plugin using Ripgrep in order to search occurrences in multiple files. Exactly like ripgrep combined with fzf (see above) with one difference: you can rename occurrences in multiple files, using the famous [vim substitution](http://vim.wikia.com/wiki/Search_and_replace) on the files you selected for one precise occurrence.

Say you need to rename a variable through a bunch of files? This plugin can do that for you very easily. Moreover it gives you full control of what you are modifying.
 
I had a lot of problems with PhpStorm batch renaming, specially because it has tendency to replace stuff I don't want to replace.
I never had one problem with this plugin.

### Code Quality plugin

Do you make mistakes when you write your code?
If the answer is no, please let's get in touch. I would love to know how you do it... 

Otherwise I strongly advise you to get this plugin:

* [neomake/neomake](https://github.com/neomake/neomake)

Neomake will populate the gutter on the left (near the line numbers) and the [window's location list](https://noahfrederick.com/log/a-list-of-vims-lists) for anything wrong in your code, thanks to warning makers. 

These warning makers are plugged to some well-known PHP code quality tools:

* [PHP Mess Detector](https://phpmd.org/)
* [PHP Static Analysis Tool](https://github.com/phpstan/phpstan)
* [PHP Code Sniffer](https://github.com/squizlabs/PHP_CodeSniffer)

You can as well create your own makers. In Vim just enter `:help neomake-job-makers` and follow the instructions.

Neomake can display as well any PHP syntax errors out of the box.

### Refactoring plugins

Having some  tools for refactoring and correctly formatting your code will save you a lot of time.

* [adoy/vim-php-refactoring-toolbox](https://github.com/adoy/vim-php-refactoring-toolbox)

Via simple keystrokes you can (obviously) configure, this plugin can:

* Rename local variables, methods, class properties
* Create getters and setters or class properties
* Extract use statement, constants, class properties

This list is not exhaustive. A very well done plugin easy to configure.

* [phpactor/phpactor](http://phpactor.github.io/phpactor/vim-plugin.html)

As stated above, this plugin do some very useful things like:

* Moving a class in your project tree and update all its references (!)
* Automatically create properties from constructor arguments
* Extract method
* Extract constant
* ... and much more

A beast.

### Git plugins

Who doesn't use [git](https://git-scm.com/) nowadays? If you prefer using SVN, this is a lie. Try git and never look back.

* [tpope/vim-fugitive](https://github.com/tpope/vim-fugitive)

Vim-fugitive is a must have. You can do a lot with it (git blame or displaying a diff on a specific file...). Highly recommended.

* [mhinz/vim-signify](https://github.com/mhinz/vim-signify)

Vim signify is very useful as well: it shows you what lines were modified / added / deleted by displaying little signs in the left gutter.

### Snippets plugins

What would I do without [snippets](https://en.wikipedia.org/wiki/Snippet_(programming))? It is very convenient in order to speed up your code typing. Do you want to create a private method by only typing `pri` followed by a keystroke? I'm sure you do.

* [SirVer/ultisnips](https://github.com/sirver/UltiSnips)


Ultisnips will provide you a very good and fast snippet engine working with every languages you want.

 * [honza/vim-snippets](https://github.com/honza/vim-snippets) 

Vim-snippets is a very good set of snippets for a lot of languages, including PHP or course.

The best part: it is very easy to extend with your own snippets!

### Outline plugins

An outline plugin display a list of every methods and properties of a class in a separate window. You can then select them to jump to their definition in the source file itself.

* [majutsushi/tagbar](https://github.com/majutsushi/tagbar)

This is the best outline plugin you will find. I personally don't use it often (and I constantly forget about it) but it can be handy in some occasions.

### Debugger plugin
* [joonty/vdebug](https://github.com/joonty/vdebug)

If you need a good debugger working with many languages, vdebug is the best you can find. It can be slightly tedious to configure but it is very powerful. For PHP you need to have [xdebug](https://xdebug.org/) properly configured to use it.

For [Docker](https://www.docker.com/) users (or if you use any virtual machine like [Vagrant](https://www.vagrantup.com/)) you need to specify the path of your project as follow:

```vim
let g:vdebug_options["path_maps"] = {
    \       "/mySuperProject": "/home/mySuperUser/workspace/mySuperProject"
    \}
```

The path on the left is the one on your virtual machine / Docker, the one on the right is your local path.

### PHPDoc plugins

It's very convenient to be able to generate PHPDoc via a simple keystroke. It's exactly what do these plugins!

* [tobyS/vmustache](https://github.com/tobyS/vmustache)
* [tobyS/pdv](https://github.com/tobyS/pdv)

These plugins will create automatically some useful PHPDoc with argument variables (but not returns). You will most of the time need to complete the information but it's a good start.

## In a nutshell: Vim better than PhpStorm?

I don't like to go in this kind of comparison: PhpStorm is an IDE which has much more features. To me, it has too many, I hate having one million windows and pop up which disturb my thinking and my flow.

The command line is more versatile and powerful than a bunch of windows.

Vim is maybe not "better" than PhpStorm but I argue that Vim can definitely replace PhpStorm with the plugins listed in this article. 

Moreover you have the flexibility of Vim configuration and you can extend it as you wish. PhpStorm doesn't really give you any configuration choices (at least only basic ones).

To me at least, Vim is indeed more interesting to use and more powerful than any other PHP IDE I've ever tried!

----

## Quick reference

* Plugin manager
    * [junegunn/vim-plug](https://github.com/junegunn/vim-plug)
* ctags generation
    * [ludovicchabant/vim-gutentags](https://github.com/ludovicchabant/vim-gutentags)
* Essentials plugins
    * [scrooloose/nerdtree](https://github.com/scrooloose/nerdtree)
    * [bfredl/nvim-miniyank](https://github.com/bfredl/nvim-miniyank)
    * [moll/vim-bbye](https://github.com/moll/vim-bbye)
    * [itchyny/lightline.vim](https://github.com/itchyny/lightline.vim)
    * [tpope/vim-commentary](https://github.com/tpope/vim-commentary)
    * [tpope/vim-abolish](https://github.com/tpope/tpope-vim-abolish)
* Project management plugins
    * [amiorin/vim-project](https://github.com/amiorin/vim-project)
    * [mhinz/vim-startify](https://github.com/mhinz/vim-startify)
* Syntax plugins
    * [StanAngeloff/php.vim](https://github.com/StanAngeloff/php.vim)
    * [stephpy/vim-php-cs-fixer](https://github.com/stephpy/vim-php-cs-fixer)
* Autocompletion plugins
    * [ncm2/ncm2](https://github.com/ncm2/ncm2)
    * [phpactor/phpactor](https://github.com/phpactor/phpactor)
    * [phpactor/ncm2-phpactor](https://github.com/phpactor/ncm2-phpactor)
* Searching / replacing plugins & tools
    * [https://github.com/junegunn/fzf](https://github.com/junegunn/fzf)
    * [junegunn/fzf.vim](https://github.com/junegunn/fzf.vim)
    * [https://github.com/BurntSushi/ripgrep](https://github.com/BurntSushi/ripgrep)
    * [https://github.com/wincent/ferret](https://github.com/wincent/ferret)
* Code Quality plugin
    * [neomake/neomake](https://github.com/neomake/neomake)
* Refactoring / code styling plugins
    * [adoy/vim-php-refactoring-toolbox](https://github.com/adoy/vim-php-refactoring-toolbox)
    * [stephpy/vim-php-cs-fixer](https://github.com/stephpy/vim-php-cs-fixer)
    * [phpactor/phpactor](https://github.com/phpactor/phpactor)
* Git plugins
    * [tpope/vim-fugitive](https://github.com/tpope/vim-fugitive)
    * [mhinz/vim-signify](https://github.com/mhinz/vim-signify)
* Snippet plugins
    * [SirVer/ultisnips](https://github.com/sirver/UltiSnips)
    * [honza/vim-snippets](https://github.com/honza/vim-snippets) 
* Outline plugin
    *  [majutsushi/tagbar](https://github.com/majutsushi/tagbar)
* Debugger plugin    
    * [joonty/vdebug](https://github.com/joonty/vdebug)
* PHPDoc generation plugins 
    * [tobyS/vmustache](https://github.com/tobyS/vmustache)
    * [tobyS/pdv](https://github.com/tobyS/pdv)