DEV Community

Cover image for Setting Up Vim and the PHP Language Server Protocol

Setting Up Vim and the PHP Language Server Protocol

camilo profile image Camilo Payan ・2 min read

Vim is great at editing text, but doesn't have the full language features built
in that devs love in IDEs. At the same time, the Vim plugin ecosystem has
allowed for new languages to quickly ramp up in Vim instead of waiting for
adoption to support a full IDE.

Enter the Language Server Protocol. Originally to support another text editor,
Visual Studio Code, LSP has been embraced by the Vim world as a way to get much
richer language support than we're used to, and now that's available for PHP!

Let's step through how to install Language Server Protocol support for PHP in


Some silly assumptions to start!

  • You have Vim installed and a method of installing plugins you prefer. Why? Because there are multiple plugin managers and I'd like to gloss over instructions for each.
  • You have Composer installed for your PHP project
  • You're comfortable on the command line! This seems likely if you're using Vim.

Step 1: Install the LSP client plugin

The most popular Vim plugin for LSP integration is Prabir Shrestha's Vim-LSP
. Go ahead and install this
plugin based on your plugin manager of choice and the instructions at the Github
link. This is your LSP client for Vim!

Step 2: Install the LSP Server

In your project, run:

./vendor/bin/composer require -D felixfbecker/language-server

This will install the language server locally to your project, and add it to
your composer.json dev dependencies. Why do this per project? I have a few
projects and generally avoid depending on a global install if I can help it. If
you have different requirements, you could install it with a global composer

Step 3: Parse stubs

There are plenty of parts of PHP that aren't defined in your code but the
language server still needs to know about! These are things that are in the PHP
standard library, like definitions for DateTime or function signatures for built
in functions like array_map.

From your project, run:

./vendor/bin/composer run-script --working-dir=vendor/felixfbecker/language-server parse-stubs

Step 4: Configure Vim to start and use the LSP

Now we need to configure Vim to use the server. Add this to your vimrc

au User lsp_setup call lsp#register_server({
    \ 'name': 'php',
    \ 'cmd': {server_info->['php',
expand('[PATH TO YOUR COMPOSER VENDOR DIRECTORY]/vendor/bin/php-language-server.php')]},
    \ 'whitelist': ['php'],
    \ })

autocmd FileType php setlocal omnifunc=lsp#complete

Replace the path with your path. I use Vim's support for directory vimrc using
exrc so I replace that path portion with .. But if you have a different
setup, you'll need to make sure it points to the vendor directory where you
installed the language server earlier.

Step 5: Test it out!

Go into your project, and try autocompleting a class or function name using
Ctrl+X Ctrl+O, the keyboard shortcut for Vim omnicomplete.

Discussion (0)

Editor guide