How to set up Emacs as a PHP Editor

Emacs editing PHP

One of Emacs' biggest advantages is its extendability. There are packages for just about everything, from code editing and compilation to browsing websites like Reddit.

Every setup is a little different, but this outlines the extensions I've been using to write PHP code with Emacs. So far I've found it to cover about 99% of what I need to do my job.

Essential Packages


This is all you really need to get started. It adds syntax highlighting and sets up indentation. It doesn't work so great for HTML formatting (especially when mixing PHP/HTML), so I recommend using a dedicated mode for that (such as web-mode below).

Note: The version on SourceForge is out of date. The fork from GitHub is still maintained and is the version provided by melpa.


I only discovered this recently, but it's quickly become my favourite mode for web development. The HTML support is great, and it also works well when editing template languages such as Twig, Liquid or Ruby's erb format.

Emacs web-mode

web-mode also provides a lot of keyboard shortcuts and adds functionality for collapsing tags, renaming elements and navigating around a document quickly.

I use the following configuration, which makes web-mode the default mode for editing templates used by the Zend and Symfony2 frameworks.

(add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.html\\.twig\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))


Checks your PHP code for syntax errors on the fly using PHP's built in syntax checker. It can also be configured to use phpcs and phpmd.

Note: I previously recommended flymake, but flycheck has been quicker and more stable for me.

Recommended Packages


Helm is an Emacs framework quickly narrowing selections. It looks a little like this:

Using Helm

It integrates nicely with some other packages (including projectile) which makes moving around files much quicker.


Projectile has a long list of excellent features, but the biggest time savers for me have been:

  • Jumping to any file in a project using helm
  • Quickly moving between code and test files
  • Running project tests with a single command

For a more detailed look at projectile's features, see "Exploring my Emacs packages - projectile".


Adds snippet expansion to Emacs. Great for speeding up creation of boilerplate code. Comes with a selection of snippets for about two-dozen languages, although PHP is not one of them. However, it's easy to add your own snippets and something like php-auto-yasnippets can generate snippet definitions for all PHP functions.


Adds some extra commands for running PHPUnit tests within the buffer. Hasn't been updated in a while.


A very robust package that adds automatic completion. A big timesaver.


Makes working with brackets and braces a whole lot easier. Automatically closes brackets and can handle deletions as well.


Great for editing behat test files. Couple this with YASnippet and writing feature tests takes no time at all.


tramp is a great extension for working with files on another machine. You can connect using several protocols, including SSH, and edit files as if they were on your local machine.


Adds the ability to quickly run the contents of a buffer with C-c C-c. You can even replace the code in a region with the result of running it.


Adds functionality for expanding a selection in semantic units. This comes in handy when editing code, as you can expand a region to enclose the current if block, or capture an entire function in just a few keystrokes. Emacs Rocks has a great video about expand-region that shows just how useful this can be.


This is a great little extension that lets you execute HTTP requests from within a buffer. The results are then shown in a new buffer (along with the HTTP response headers), and any JSON or XML results will be nicely formatted as well.

Running REST requests in Emacs

This is extremely useful when writing and debugging API's, and makes finding problems a lot easier.

There are a couple of other useful extensions in the Emacs Extensions guide.

Useful keyboard commands

Emacs is not exactly short of keyboard commands, but there are a few that I find myself using over and over again.

php-mode shortcuts

Command What it does
C-. Displays arguments for a method (requires a valid TAGS file)
C-c C-f Searches the PHP manual for the keyword at the current point

web-mode shortcuts

Command What it does
C-c C-f Fold / unfold the current tag
C-c C-e r Rename the current element, including its closing tag.
C-c C-i Indent the entire buffer
C-c C-t b Jump to beginning of the current tag
C-c C-t e Jump to the end of the current tag
C-c / Close the currently open tag

Running Phing

Emacs has the compile command that will run make in the current directory. This isn't so great for PHP, but it's not too difficult to get it working happily with something like Phing.

phing-call.el is based on Ant call. It will ask for a target name to execute, and will then search the directory of the current file for a build.xml file. If no build.xml is found, it will search in the parent directory/directories until it finds one. The output will then be shown in a buffer named *phing-compilation*.

Guide Changelog


  • Updated projectile section with some more detail


  • Tidied up description of php-mode
  • Replaced flymake with flycheck
  • Replaced auto-complete with company-mode
  • Added helm and projectile to the list of recommended packages


  • Initial publication