Auto formatting a PHP file in emacs

2020-08-01 05:08发布

问题:

I come from an Eclipse/Vim background, and I've just started using Emacs for the first time for editing my PHP files.

I've recently taken over a chunk of PHP files, and the existing code formatting is inconsistent and *very* poor.

if (condition   &&      condition)      {    
    }

if (condition   &&      condition)      
{

$var            =   "val"        // comment

    }
$var2      =           "val2"       // comment
//...

In Eclipse and Vim, I can format source files(Java and C/C++ respectively), and the IDE will take care of indentation, code style, and code cleanup. This means removing unnecessary white space, bracing properly, inserting/removing newlines where appropriate, etc.

How do I do this in Emacs with PHP files? I'm using PHP-mode in nxhtml, and I've figured out the indentation portion, so all my PHP is indented(tabbed) properly, but the rest of the code is still out of whack.

For example, in the above code, how do I specify brace style and spacing rules?

回答1:

Maybe PHP Beautifier PEAR package would do what you need? E.g. just use the command line tool on the files and make them proper.



回答2:

Emacs' php modes can only do indentation for you. You need to run it through some external tool to fix that formatting.



标签: php emacs format