Minimalist Sublime Text 3 Setup for PHP

Developer Tools

November 1st, 2018

Minimalist Sublime Text 3 Setup for PHP

While many developers are flocking to IDEs and other hybrids like Visual Studio Code, I’ve found myself trying to create a minimalist Sublime Text 3 setup. I am a minimalist at heart, and I like both my physical environment and virtual environment to be as minimal and tidy as possible.

I want to present to you the minimalist Sublime Text 3 setup that I’ve landed on, but still feel like I have most everything I need to be productive.

Built-In Features

The built-in feature I am most crazy about is the Goto -> Goto Definition.. and Goto -> Goto Reference:

Sublime’s code intelligence has improved drastically since earlier versions, and I don’t need PHP Companion for code intelligence like navigating to the source code of a method call.

I use the shortcut ⌥⌘↓ to go to definition dozens if not hundreds of times per day. If I need a quick reference of the method signature, this shortcut does the trick, and I can quickly jump back and forth between the definition and the current file with ⌘P⏎.

The Goto Anything and immediate hitting enter is a quick way to jump between two files for me.

If you are at a method definition you can use the Goto Reference to get a list of possible reference calls, in this example the Application::run() method:

The most apparent built-in feature you’ll use hundreds of times a day is Goto Anything with ⌘P. I use this all the time.

Additionally, I use Goto Symbol in Project dozens of times a day if I think I know the method name, but I need to confirm it.

I find that ditching the IDE has made my mind sharper when it comes to remembering library APIs I use every day. Each time I go for a method, instead of relying on auto-complete, I have a mini-quiz to see if I know the method name by heart.

If I end up forgetting the method name, I quickly jump to the class and search or use Goto Symbol to see a list of methods.

Last, I love this keybinding that is not configured by default, but is available out-of-the-box:

{ "keys": ["ctrl+shift+r"], "command": "reveal_in_side_bar"}

With this key binding Control + Shift + r reveals the file I am currently working on in the sidebar. I like to use this to get a sense of the folder layout and other related classes of the current file I’m editing.

PHP Companion

Apart from Laravel-specific plugins like blade syntax highlighting (I will list my complete list of plugins later), PHP Companion is probably the only essential PHP plugin I need for Sublime.

I mostly use it for two features:

  1. Importing a namespace
  2. Expanding the full namespace of a class

I have these commands at my fingertips with keyboard shortcuts:

[
{ "keys": ["alt+t"], "command": "run_phpunit_test"},
{ "keys": ["super+alt+t"], "command": "run_single_phpunit_test"},
{ "keys": ["super+alt+l+t"], "command": "run_last_phpunit_test"},
{ "keys": ["super+shift+t"], "command": "run_phpunit_tests_in_dir"},
{ "keys": ["super+shift+ctrl+t"], "command": "run_all_phpunit_tests"},
{ "keys": ["super+alt+enter"], "command": "expand_fqcn" },
{ "keys": ["alt+enter"], "command": "find_use" },
{ "keys": ["f7"], "command": "insert_php_constructor_property" },
{ "keys": ["alt+shift+i"], "command": "import_namespace" },
{ "keys": ["ctrl+shift+r"], "command": "reveal_in_side_bar"}
]

Specifically, PHP Companion gives me the alt+enter to import a namespace at the top of the file, and if I need to expand the full namespace of a class I use super+alt+enter. I use the latter when I am defining @param ’s for methods.

The above snippet is my full list of keyboard bindings. I try to keep them minimal as well, and only a handful that I use over and over.

CLI Access

I like to open my code from the command line where I manage source control, or another way I sometimes open projects is through GitHub Desktop with Sublime configured as my editor.

I typically have ~/bin in my $PATH on Linux/Unix systems, so I symlink the subl CLI as follows:

ln -s \
"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" \
/usr/local/bin/subl

Now from any folder, I can open it in Sublime as follows:

cd ~/Code/my-project
subl .

I also run my PHPUnit tests a lot during the day, so I use Adam Wathan’s Sublime PHPUnit plugin to run individual tests. It saves a ton of time as opposed to using something like the phpunit --filter flag.

Plugins

Here’s my complete list of plugins installed with Package Control:

  • AdvancedNewFile
  • DocBlockr
  • Dockerfile Syntax Highlighting
  • Laravel Blade Highlighter
  • PHPCompanion
  • Vuetify
  • Sublime PHPUnit (not in Package Control)

And that’s it!

Like I said at the outset, I like to keep things running as smooth as possible and as minimal as possible. I was using the Emmet plugin, but I found that it interferes with my Goto Definition shortcuts at the time of writing. The Goto Definition shortcut is my most used shortcut, so Emmet had to go.

Snippets

I tend to create a collection of snippets as I reach for them often. For example, on one project I was building a bunch of database table schemas and factory definitions by hand (long story), so I created a factory snippet for quickly creating a factory definition and states:

<snippet>
<content><![CDATA[
\$factory->define($1, function (Faker \$faker) {
return [
$0
];
});
]]></content>
<tabTrigger>factory</tabTrigger>
<scope>source.php</scope>
</snippet>

And I can define a factory state with factorystate:

<snippet>
<content><![CDATA[
\$factory->state($1, '${2:state}', function (Faker \$faker) {
return [
$0
];
});
]]></content>
<tabTrigger>factorystate</tabTrigger>
<scope>source.php</scope>
</snippet>

These are just a few examples that I use a ton, so I have snippets. I don’t have very many snippets. I don’t tend to install packages that contain snippets; I prefer to create a handful of them that I know I’ll use.

The four snippets I use the most include shortcuts for creating class methods as well as a PHPUnit test method.

First, I use the following daily to create public methods quickly with the pubf tab trigger (which matches PhpStorm’s shortcut):

<snippet>
<content><![CDATA[
public function $1()
{
$0
}
]]></content>
<tabTrigger>pubf</tabTrigger>
<scope>source.php</scope>
</snippet>

For protected methods, I use prof:

<snippet>
<content><![CDATA[
protected function $1()
{
$0
}
]]></content>
<tabTrigger>prof</tabTrigger>
<scope>source.php</scope>
</snippet>

And finally for private methods, I use prif:

<snippet>
<content><![CDATA[
private function $1()
{
$0
}
]]></content>
<tabTrigger>prif</tabTrigger>
<scope>source.php</scope>
</snippet>

For creating a new PHPUnit test method, I use test as my tab trigger:

<snippet>
<content><![CDATA[
/** @test */
public function $1()
{
$0
}
]]></content>
<description>Create a PHPUnit Test Case</description>
<tabTrigger>test</tabTrigger>
<scope>source.php</scope>
</snippet>

Code Linting

I don’t use a PHP code linter in Sublime, however, I do run CS on my code before merging pull requests, and sometimes on the whole codebase to do a little cleanup. I don’t want anything to drag my editor down, and use my console tools to clean up code style issues ¯\_(ツ)_/¯.

Learn More

Well, that’s it.

I have always been a text editor user, all the way back to the Textmate 1 days. As IDEs started getting better, I converted to PhpStorm for a while. I wasn’t happy with the experience. Fans ablaze, memory usage galore, and it just wasn’t for me. In fact, for me, an IDE felt like training wheels for code editing.

I am glad I made the switch back from an IDE setup to a super minimal Sublime setup that I am delighted with! I feel that I have better command over the code I work with, as well as a more familiar understanding of the APIs I consume daily.

The only thing I’d say is lacking is a good XDebug experience. I don’t mind firing up PhpStorm if I want to reach for XDebug. I pay for the PhpStorm license as a glorified XDebug client, and it does have the best debugging experience out there in my opinion :)

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.