sponsor Vim development Vim logo Vim Book Ad

PHP-correct-Indenting : The official VIm indent script for PHP

 script karma  Rating 1042/435, Downloaded by 21492  Comments, bugs, improvements  Vim wiki

created by
John Wellesz
script type
GitHub: https://github.com/2072/PHP-Indenting-for-VIm

(The version 1.39 of this script is included in VIM 7.4)

- Indents PHP code as "perfectly" as possible.
- Comments' content or non-PHP code are ignored and don't break the algorithm.
- Non-PHP code, multi-line /**/ comments and "Here Document" content remain unchanged.
- HTML <Script.*> tags' content is indented as PHP and like if it was part of the surrounding PHP code.
- Fast optimized indenting, the script knows when it's indenting large blocks and skips some unnecessary tests.
- Support folding (fold markers don't break the indenting)
- Auto-formatting for comments (using the Vim option formatoptions=qroc).
- Indent correctly complex "non bracketed blocks":

    Example: (start of line spaces are replaced by ____)
if (!isset($History_lst_sel))
____if (!isset($History_lst_sel))
________if (!isset($History_lst_sel)) {
________} else
_______$bla= "foo";
$command_hist = TRUE;

- Direct indenting, the text is indented as you type it.
- switch/case are indented correctly.
- Well commented source code.
- Bug free as far as I know.

A more complete example of its capabilities:

if ($foo="thing") // as you see an opened bracket isn't needed to indent correctly :)
____if (foo("something"))

<title>SOME HTML CODE</title>
________echo "Something here";
________<script type="text/javascript">
________// Note the indentation of the script as if part of PHP

________var truc=0;
________if (truc) {



<b>some closing and oppening bracket here } } {
nothing to indent here



if (foo)
____/* a useless comment here
____with useless code:
____if (broken code) {
____if (testHereDocument()) {

________echo <<<HEREDOC
Here is an Here Document style
echo, very useful.
The following code isn't indented
if (test)
while (foo) {



That's all folks!

This script can also be found there: (with a colorful example)

Enjoy :)
install details
Just make sure the name of the file is php.vim and copy it under your vimfiles/indent folder.
PHP syntax coloring must be turned on.

For alternative install solutions (Pathogen and Vundle) please read https://github.com/2072/PHP-Indenting-for-VIm/blob/master/README.md

Available options:

Just type :help php-indent

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
php.vim 1.70 2019-07-22 8.0 John Wellesz This page is no longer updated, see the Github page of this project for future releases:

php.vim 1.52 2014-06-16 7.4 John Wellesz - Fix issue #34 where indentation could get wrong with arrays defined using the short [] declaration.

- Fix an edge case in conditional block declarations when the ')' of the condition is put on the same line as the following '{' (complement to issue #4)

php.vim 1.49 2014-04-15 7.4 John Wellesz - Added support for 'finally', 'use', 'trait' and 'final' keywords

- Implemented support for multi-line block declarations (issue #4).

- Added support (with some restrictions) for multi-line string declarations (issue #22).

- Fix issue with lines following a {} block defined on a single line if this {} block was just beneath a '{'

- Fix issue #32 ('case:/default:' indentation issues in complex 'switch' blocks)

- Fix issue #17 where closures' content would get extra indenting.

- Fix handing of ^}\s*else\n{ blocks which were not detected as new blocks and resulted in wrong indentation.

- Fix issue #23 where the script could hang in some specific cases involving closing braces at column 0;

- Fix issue #6 where nested switches would not indent correctly.

- No longer add 'w' to formatoptions VIm' setting as no other file-type plug-in uses it by default. This prevents leaving trailing white spaces when text wrapping.
php-indent_1-39.zip 1.39 2013-08-08 7.3 John Wellesz - Also add 'StorageClass' syntax identifier (with an upper-case 'C') as it also exists in the syntax file.
php-indent_1-38.zip 1.38 2013-08-02 7.3 John Wellesz Changes: 1.38
- Fix an incredibly old bug that managed to survive unnoticed until today: the
  PHP code identifier routine was missing a few syntax names (Define,
  Structure, Storageclass and Exception). If you started indenting on such a
  line, nothing would happen as the script thought it wasn't actual PHP code...

Changes: 1.37
- Fix a bug for inline script element [imicky]
- Fix issue #11: https://github.com/2072/PHP-Indenting-for-VIm/issues/11

Changes: 1.36
- Added support for short array declaration (Thanks to Warren Seymour)

Changes: 1.35
- New option: PHP_outdentSLComments to add extra indentation to single-line
php.vim 1.34 2011-02-14 7.0 John Wellesz - Fix: string with /* would be treated as comment start when using single quote. (Thanks to Manic Chuang for the fix)
php.vim 1.33_fixed 2010-10-05 7.3 John Wellesz Fixes a formatting issue concerning php.vim itself, (CRLF back to LF)
php.vim 1.33 2010-07-30 7.0 John Wellesz - Rewrote Switch(){case:default:} handling from scratch in a simpler more logical and infallible way.
- Added a new option: PHP_outdentphpescape to indent PHP tags as the surrounding code.
php.vim 1.30 2008-11-22 7.0 John Wellesz - Fixed empty case/default identation again :/
- The ResetOptions() function will be called each time the ftplugin calls this script, previously it was executed on BufWinEnter and Syntax events.
php.vim 1.29 2008-10-26 7.0 John Wellesz Fixed php file detection for ResetOptions() used for comments formatting. It now uses the same tests as filetype.vim. ResetOptions() will be correctly called for *.phtml, *.ctp and *.inc files.
php.vim 1.28 2008-06-10 7.0 John Wellesz - Added support for NOWDOC tags ($foo = <<<'bar')
- New option: PHP_vintage_case_default_indent (default off)
- End HEREDOC delimiters were not considered as such if they were not followed by a ';'.
- if a "case" was preceded by another "case" on the previous line, the second "case" was indented incorrectly.
- '/*' character sequences found on a line starting by a '#' were not dismissed by the indenting algorithm and could cause indentation problem in some cases.
- Fix some indentation errors on multi line conditions and multi line statements.
- Fix when array indenting is broken and a closing ');' is placed at the start of the line, following lines will be indented correctly.
- Minor fixes and optimizations.
php.vim 1.24 2007-02-24 7.0 John Wellesz - Added compatibility with the latest version of php.vim syntax file by Peter Hodge (http://www.vim.org/scripts/script.php?script_id=1571)
This fixes wrong indentation and ultra-slow indenting on large php files...
- fixed spelling in comments
php.vim 1.23 2006-01-14 6.0 John Wellesz - <script> html tags are now correctly indented the same way their content is.
- <?.*?> (on a single line) php declarations are now always considered as non-php code and let untouched.
php.vim 1.22 2006-01-12 6.0 John Wellesz - PHPDoc comments are now indented according to the surrounding code.
- This is also true for '/* */' multi-line comments when the second line begins by a '*'.
- Single line '/* */' comments are also indented.
php.vim 1.21 2006-01-07 6.0 John Wellesz - 'try' and 'catch' were not registered as block starters so the '{' after a 'try' or 'catch' could be wrongly indented...
(thanks to Gert Muller for finding this issue)
php.vim 1.20 2005-11-21 6.0 John Wellesz - Line beginning by a single or double quote followed by a space would cause problems... this was related to the bug correction of version 1.10 - Thanks to David F. for finding this (he was lucky).
- Changed the way this script set the 'formatoptions' setting, now it uses '-=' and '+='
- New option: PHP_autoformatcomment (defaults to 1), if set to 0 the 'formatoptions' setting will not be altered.
- When PHP_autoformatcomment is not 0, the 'comments' setting is set to the type of comments that PHP supports.
php.vim 1.19 2005-11-18 6.0 John Wellesz - Indentation of '*/' delimiter of '/**/' won't be broken by strings or '//' comments containing the "/*" character sequence.
- I Forgot to register 'interface' and 'abstract' as block starters so the '{' after them could be wrongly indented...
php.vim 1.181 2005-09-16 6.0 John Wellesz - I Forgot to register 'class' as a block starter so the '{' after a 'class' could be wrongly indented...
php.vim 1.18 2005-09-15 6.0 John Wellesz - No more problem with Vim 6.3 and UTF-8.
- Opening braces "{" are always indented according to their block starter.
php.vim 1.17 2005-07-06 6.0 John Wellesz - Now starting and ending '*' of multiline '/* */' comments are aligned on the '*' of the '/*' comment starter.
- Some code improvements that make indentation faster.
- Now following parts of split lines are indented:
    Instead of:

    You have:

- If a "case : break;" was declared on a single line, the following "case" was not indented correctly.
- If a </script> html tag was preceded by a "?>" it wasn't indented.
- Some other minor corrections and improvements.
php.vim 1.15 2005-06-23 6.0 John Wellesz - Corrected some problems with the indentation of multiline "array()" declarations.
- Added auto-formatting for comments (using the Vim option formatoptions=qroc).
- Added the script option PHP_BracesAtCodeLevel to indent the '{' and '}' at the same level than the code they contain.
- Some code cleaning and typo corrections (Thanks to Emanuele Giaquinta for his patches)
php.vim 1.12 2005-05-31 6.0 John Wellesz - The bug involving searchpair() and utf-8 encoding in Vim 6.3 will not make this script to hang but you'll have to be careful to not write '/* */' comments with other '/*' inside the comments else the indentation won't be correct.
NOTE: This is true only if you are using utf-8 and vim 6.3.
php.vim 1.11 2005-05-17 6.0 John Wellesz If the "case" of a "switch" wasn't alone on its line and if the "switch" was at col 0 (or at default indenting) the lines following the "case" were not indented.
php.vim 1.10 2005-02-22 6.0 John Wellesz Lines beginning by a single or double quote were not indented in some cases.
php.vim 1.09 2005-02-07 6.0 John Wellesz The javaScript code was not always directly indented when typing.
php.vim 1.08 2005-01-09 6.0 John Wellesz - End comment tags '*/' are indented like start tags '/*'.
- When typing a multiline comment, '}' are indented according to other commented '{'.
- Added a new option 'PHP_removeCRwhenUnix' to automatically remove CR at end of lines when the file format is Unix.
- Changed the file format of this very file to Unix.
- This version seems to correct several issues some people had with 1.07.
php.vim 1.07 2004-12-27 6.0 John Wellesz - Added support for "Here document" tags:
-   HereDoc end tag are indented properly.
-   HereDoc content remain unchanged.
- All the code that is outside PHP delimiter remains unchanged.
- New feature: The content of <script.*> html tag is considered as PHP and indented according to the surrounding PHP code.
- "else if" are detected as "elseif".
- Multiline /**/ are indented when the user type it but remain unchanged when indenting from their beginning.
- Fixed indenting of // and # comments.
- php_sync_method option is set to 0 (fromstart). This is required for complex php script else the indent may fail.
- Files with non PHP code at the beginning could alter the indent of the following PHP code.
- Other minor improvments and corrections.
php.vim 1.06 2004-12-19 6.0 John Wellesz - Switch block were no longer indented correctly...
- Added an option to use a default indenting instead of 0.
- A problem with ^\s*);\= lines where ending a non '{}' structure.
- Changed script local variable to be buffer local variable instead.
php.vim 1.05 2004-12-15 6.0 John Wellesz Lines containing "<?php.* ?>" and "?> .*<?php" (start and end tag on the same line) are no longer indented at col 1 but as normal code.
php.vim 1.0 2004-11-01 6.0 John Wellesz Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Vim at Github