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.
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
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.
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
Note: I previously recommended flymake, but flycheck has been quicker and more stable for me.
Helm is an Emacs framework quickly narrowing selections. It looks a little like this:
It integrates nicely with some other packages (including
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
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.
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.
|Command||What it does|
||Displays arguments for a method (requires a valid TAGS file)|
||Searches the PHP manual for the keyword at the current point|
|Command||What it does|
||Fold / unfold the current tag|
||Rename the current element, including its closing tag.|
||Indent the entire buffer|
||Jump to beginning of the current tag|
||Jump to the end of the current tag|
||Close the currently open tag|
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
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
If you found this article helpful, check out my book "Writing PHP with Emacs". It goes into much more detail and is regularly updated with new content.
Use this special link to get an extra $5 off your purchase.