Using Sublime Text as a full featured PHP IDE

Posted in Development, PHP, 9 months ago Reading time: 8 minutes
image

The Integrated Development Environment, or IDE in short, is one of the developer's most important tools. Most of them have fiery opinions on which one is the best. Wars have been fought about it. The most known of these is the eternal and decennia lasting war between vim and emacs users.

Within the PHP community PhpStorm is very popular, and not without reasons. People almost think you're crazy when you use something else as a PHP developer. I don't think this is justified.

I have been using Sublime Text (in short: ST) for a long time now. It dates back from the time I was still developing in Perl, and I sticked with it since then. I really like the editor and thanks to many available plugins it can match PhpStorm in functionality. On top of that, ST is much faster and less resource hungry, and it is fully language agnostic. The creators of PhpStorm have different versions for other languages, like PyCharm for Python, or IntelliJ IDEA for Java. But seriously, who wants to use a different IDE for every language?

ST has a very minimalistic interface, with no bells and whistles. I like that: no abundance of windows and panes, but just a bare bone editor, with no more than a side panel for folders and files, and a console for notices and diagnostics, which disappears with a press on ESC. And if this interface still is too crowded for you, there's the Distraction Free Mode.

ST is almost completely useable with only the keyboard. All commands are quickly accessible using the neat command palette, by pressing ctrl-shift-p. Just type a part (or just some random letters) of the command name, and you will quickly find what you're looking for,

Same goes for the file palette, accessible via ctrl-p. You will be able to find any file in your project quickly. Other editors have copied this functionality, but (as far as I know) ST was the first.

Something else I wouldn't want to live without anymore: the multi cursor. This is also something that has been copied by others. For example you can select a piece of text that occurs multiple times in your file, and press ctrl-d to select the next occurences, with an additional cursor. This way you can perform edits in multiple places at once.

I love the command line. That's why I like it that there is a command line tool available as well, subl. With it, you can quickly open a file or entire folder. Also it is possible to run any editor command directly from the command line.

A default ST installation is quite basic. It only gives you the editor. It is an excellent editor though, and has support for syntax highlighting and other features like a symbol list and (limited) auto suggest functionality, setting it apart from other editors. But ST will really shine with plugins.

All plugins can be found in Package Control. These are installable directly from within ST using the plugin with the same name, using the command palette.

Here I will showcase some plugins that I deem essential, especially for PHP developers, in more or less random order. This is not an extensive list.

Let's start with the plugin that turns ST into a full fledged IDE: LSP, short for the Language Server Protocol. Originally designed by Microsoft for VS Code. Nowadays it is an open standard and supported by many editors/IDEs like VS Code obviously, but also Atom, all Jetbrains products as well as ST.

LSP provides intelligent code analysis, giving you context aware code suggestions, extensive code linting, "goto definition", "find references" and smart refactoring tools like project wide renaming of classes, functions and variables. There are LSP implementations for almost every language. LSP is really The Bomb and an absolute Must Have for any developer. It makes your life so much easier.

This is a LSP implementation for PHP. There are other implementations as well, but this is the best. Believe me, I tried them all. It is the same implementation as in VS Code. Intelephense has a fully functional free version, and there is a paid version for a one time fee of just 15 US dollar. This will get you a licence key unlocking some very useful premium features. Definitely worth the money.

Json may not seem to be a format needing something like LSP. But don't forget that json can be schema based, which is supported by LSP-json. Very useful if you are editing something like OpenAPI json files.

But above all: all ST configuration is fully json based, and LSP-json can really help with this. It provides you with useful feedback and smart suggestions for all settings while editing the json files. In my opinion, this plugin should be part of ST by default, as the settings can be hard to edit without it.

Many other languages are supported. Like for example: LSP-typescript, LSP-css or if you are using Tailwind: LSP-tailwindcss. See Package Control for a complete list.

There are also some LSP implementations that don't have a ST plugin yet, like Perl::LanguageServer. You can use these in ST too, but you will need to manually configure them in the LSP settings.

Never open a browser again to go to the PHP documentation site. Just access all documentation right from within ST!

Syntax highlighting for Laravel blade files.

Not much more than some handy shortcuts to the Laravel documentation, but I find that I use these a lot.

This is a wrapper around multiple code analysis tools:: PHP codesniffer (phpcs), the default PHP linter (using php -l), and the PHP mess detector (phpmd). Next to this you can use this plugin to format your PHP files using php-cs-fixer or phpcbf. This can be done manually, or automatically everytime you save your file.

Phpactor (pronounced as: factor) is a powerful code analysis / refactoring tool for PHP. There is a LSP implementation and a RPC implementation, both useable in ST. I like the LSP implementation less than Intelephense, but the RPC implementation is a very useful addition to your tool belt. Phpactor offers functions like:

  • automatic detection and correction of namespace and class name

  • automatic importing of classes

  • project wide renaming of classes

  • generation / completion of constructor function based on class properties

  • generation of stubs based on contracts/interfaces

  • https://github.com/tkotosz/sublime-phpactor-plugin

Like the name suggests, this is a client for xdebug, the PHP debugging framework.

Every developer has struggled with brackets or parentheses that don't match well. By default you will get visual feedback in ST about the location of the opening or closing bracket. But that doesn't always provide enough information. BracketHighlighter makes it better and easier. This plugin also works with html tags and even user defined custom tags.

Essential if you work in a team with a coding standard. This plugin supports directives defined in a .editorconfig file, see https://editorconfig.org/.

This is a linting framework, in other words: syntax checking of your code. Less powerful than LSP but useful nevertheless. The plugin is just the framework, you will need to install linters for all languages you're using. Like for example SublimeLinter-csslint. See Package Control for the complete list.

A plugin to convert text from/to base64, base32 and base16.

Similar to the last one, this plugin will do URL encode/decode for selected text for you.

If you work with CSV files but you don't feel like starting your spreadsheet application every time, then Rainbow CSV might be something for you. It takes care of different colors for each column and shows a mouseover popup with the column name. There is even SQL like query functionality available.

Configuring your ST installation just how you like it, can take some time. You don't want to repeat this process every time you do a reinstall or a install on a different machine. SyncSettings takes care of this. It will save all your config settings to a private Github gist. On a new install, you only need the url of this gist and all settings will be synced automatically.

An alternative plugin is PackageSync. This will let you download your settings in a zip file that can be synchronized using something like Dropbox or Nextcloud.

With large projects you may end up with many open files. This plugin will keep it down to a manageable number. By pressing ctrl-alt-c (configurable) the oldest, unmodified tabs will be closed. Tabs with unsaved changes will be left alone. The amount of tabs that stay open can be configured. Next to this, you will be able to close all files from another folder than the current one.

Terminus lets you open a terminal ina ST tab/pane, or in the console. Neat, so you don't have to switch to another terminal application and stay in your IDE.

You might have noticed I did not mention any git related plugin. There are some, but they are limited in functionality. But there is Sublime Merge, a stand alone full featured git client, which provides excellent integration with ST. I like Merge so much that I hardly use the git CLI anymore. Merge is free to use but with some small limitations. For the full version you will need a licence.

Related posts

image
A new blog platform

It was about time I started writing more about my profession, after all those years. So I started a blog. I built the platform myself. So I wrote a blog post about that.

Read more →

image
PHP: Frankenstein arrays

PHP has become quite a nice language, but there are some ugly legacies left from the past. Like the deceptive Frankenstein abomination known as the "array".

Read more →

image
Pass, the standard Unix password manager

Many people nowadays are using a password manager, like LastPass, 1Password, Keepass, etc. Not many are familiar with "pass, the unix password manager". That's a shame, because I think it is the best password manager for the tech savvy linux/unix user. Let me tell you why.

Read more →