[ACCEPTED]-Formatting PHP Code within Vim-tidy

Accepted answer
Score: 76

Quick way to fix PHP indentation in vim 6 is to visually select the lines you want 5 to work with using shift-v, and then press equals 4 (=) to trigger auto-formatting.

As for other 3 formatting issues you're probably looking 2 at employing some regex search and replaces, such 1 as :%s/^M/\r/g (that's ctrl-V ctrl-m, not caret-M) to fix line endings

Score: 16

Enter normal mode in vim and then type



Score: 10

Format in PSR-2 style

For the new standard Coding Style Guide PSR-2 use the PHP-CS-Fixer.

There is 2 a Vim plugin: Vim-php-cs-fixer

How to install:

Install PHP-CS-Fixer (globally with Composer):

composer global require friendsofphp/php-cs-fixer

Then 1 add the Vim plugin (Pathogen):

cd ~/.vim/bundle
git clone git@github.com:stephpy/vim-php-cs-fixer.git

Restart Vim.

Default mapping:

<leader>pcd " For directory
<leader>pcf " For flie
Score: 8

There is a vim plugin that enables formatting 14 on your code from within vim. It's called 13 vim-autoformat and you can read about it 12 and download it here:


It integrates external 11 code-formatting programs into vim. When 10 this plugin is installed, you only have 9 to install an external code formatter to 8 get everything to work out of the box. It 7 supports the php formatter phpCB, which is the 6 best php formatter i've seen so far.

UPDATE: phpCB 5 is not supported anymore, due to code breaking 4 behaviour. However, vim's indentfile is 3 always used as fallback, allowing you to 2 at least indent your code when there's is 1 no formatter available.

Score: 2

The vim website is not the easiest to navigate, but 3 there is a wealth of chewy nougat center 2 there.

For instance I found this php indenting script there. Give 1 it a try.

More Related questions