37

In Sublime Text 2, I am trying to type the code and have auto format command to rearrange and auto indent source code. If it is not possible to have a automatic formatter, would be nice to have a shortcut command to do it.

I am searching for something else besides the built-in "Reindent" option and the hard-to-make-work "PHP Beautifier" plugin.

I would like to know if someone found any nice auto formatter for PHP: any formatter that won't mess our PHP code. JSFormat seems to be not useful.

Is there any easy to "install and use" plugin?

Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161

7 Answers7

38

PHPTidy is definitely good choice for PHP code formatting in SublimeText2.

You need to install package control package to enable you to install packaged in sublime text: link.

  1. ctrl+shift+p -> type -> Package Control: Install Package

  2. search for phpTidy -> hit enter

that is it - now you can format your code in WordPress coding convention.

How to format you code:

  1. ctrl+shift+p -> type -> tidy -> Hit Enter
  2. ctrl+alt+t

Hope this help.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Vukasin
  • 561
  • 5
  • 9
21

There is a PHP code formatter named PhpBeautifier. Here you can find a Sublime Text plugin for this program and you can also install it via package control.

Philipp Michael
  • 954
  • 1
  • 11
  • 22
  • Welcome to Stackoverflow. Nice post! Make sure however that you don't put your own questions in an answer. If you have a question or need help of your own create your own question by clicking the ask question button. Just make sure you search to make sure your question hasn't already been asked by another member. – Bailey Parker Jun 20 '12 at 08:52
  • 1
    @PhpMyCoder: OK, I have edited my answer and will ask a new question if necessary. – Philipp Michael Jun 20 '12 at 12:30
  • 2
    I knew that PhpBeautifier. After installing it on lastest Sublime Text 2, I had so many formatting issues that I uninstalled it. I am still wishing to find another plugin that formats my code without messing with it. Javascript formatters seems to work, but this PhpBeautifier drove me crazy. – Junior Mayhé Jun 22 '12 at 21:10
8

I have this set up as a keybind:

[
    {
      "keys": ["alt+shift+f"], "command": "reindent",
      "args": {"single_line": false}
    }
]

If you're looking for a bit more than that there is Tidy PHP under package management.

Update: Been some time, I'm now using PHP Coding Standards along with PHP CS Fixer which automates a lot of the cleanup done by autoformatting. This takes a fair bit of work / understanding of php cs to setup but its not too hard, and well worth it. Link

Community
  • 1
  • 1
lsl
  • 4,371
  • 3
  • 39
  • 54
  • Sadly the built in reindent can't handle single line comments with `//` and can't format arrays that are longer that one line – CodeBrauer Apr 03 '17 at 12:41
7

I've been searching this before also, but with no avail. Sublime text 2 probably doesn't support formatting PHP code natively. I have looked into ST2 packages - http://wbond.net/sublime_packages/community, but there is also nothing. Probably you'll have to do this in another editor.

Of course it would be nice, if someone would write a package for this, 'cause packages for formatting JS, JSON, HTML.. already exist.

MelkorNemesis
  • 3,415
  • 2
  • 20
  • 20
3

This is an old subject, but as I found it via Google, here's an answer :

You can use the phpfmt plugin for Sublime Text 3, and configure it to respect code conventions (Such as PSR-0, PSR-1 or PSR-2).

The files will be beautified on save.

Marc Brillault
  • 1,902
  • 4
  • 21
  • 41
2

I'm using sublimetext-codeformatter

It supports PHP, JavaScript/JSON, HTML, CSS, SCSS, Python, Visual Basic/VBScript and maybe more in the feature. For PHP formatting it uses a fork of the phpfmt which works well. All languages-specific formatting can be set in one config file.

CodeBrauer
  • 2,690
  • 1
  • 26
  • 51
0

Not sure if this is what you are looking for, but are you aware that when you save your file as .php, Sublime Text 2 automatically highlights and indents according to its built in PHP rules. You can also select this from going to View, and then Syntax, and selecting PHP. But again, this is automatically done when you save the file as .php.

BigHeadCreations
  • 1,583
  • 3
  • 16
  • 31