Home » Php » sublimetext2 – Is it possible to auto format PHP in Sublime Text 2?

sublimetext2 – Is it possible to auto format PHP in Sublime Text 2?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.

Answer:

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.

Answer:

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.

Answer:

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

Answer:

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.

Answer:

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.

Answer:

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.