One of the things you’ll find annoying if you’re trying to adhere to the PSR standards is that it’s hard to notice things like extra spaces at the end of lines and that you don’t have a blank line at the end of your file. You can run something like phpcbf to fix the file before you commit (and that’s a perfect option which will discuss in another article) but it’s nice to have sublime clean up whenever possible before you save.

The EditorConfig file is a file format that allows you to define how the editor should save files of specific types. Sublime Text has support through a plug-in named conveniently enough EditorConfig.

Install the Plug-in

Step one is to install the plug-in. You should already have Package Control installed from earlier so we’re going to use the nice process of:

  1. command+shift+p
  2. Type “Package Control: Install Package” (or you can copy and paste what I just typed)
  3. Type “EditorConfig”
  4. Press “Return”

Add a .editorconfig

To do this we’re going to create a new file named “.editorconfig” at the root of our project and paste in the following:

# editorconfig.org
root = true

[*.php]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true

Now when we work on .php files the editor will automatically indent using 4 spaces (indent_style and indent_size), use Unix style end of lines (end_of_line), keep an empty line at the end of the file (insert_final_newline), save using utf-8 (charset), and remove any extra whitespace at the end of a line (trim_trailing_whitespace).

Generally, I try to stick to to these same standards for my JS and SASS files so normally I’ll do:

# editorconfig.org
root = true

[*.{php,css,js,scss}]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true