Home page

616,974 Visitors, Friday 09 December 2016 18:16:37 CET, IP:

Return to parent page: Miscellaneous

PHP Indentation VIm script

This is the official PHP indentation plug-in for VIm (version 1.60 is bundled with VIm 8.0).

Download here (version 1.61 released on 2016-10-02 - a complete changelog is available)

You can subscribe to a newsletter about this script. (Only new releases will be announced there)

Development versions are available on GitHub: https://github.com/2072/PHP-Indenting-for-VIm (this is also the place to report issues)

- 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":

if (!isset($History_lst_sel))
    if (!isset($History_lst_sel))
        if (!isset($History_lst_sel)) {
        } else
$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 oppenning 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) {



Available options:

(type :help php-indent inside VIm to see the following and more)

NOTE:   PHP files will be indented correctly only if PHP syntax is active.

If you are editing a file in Unix 'fileformat' and '\r' characters are present
before new lines, indentation won't proceed correctly ; you have to remove
those useless characters first with a command like: >

    :%s /\r$//g

Or, you can simply :let the variable PHP_removeCRwhenUnix to 1 and the
script will silently remove them when Vim loads a PHP file (at eachBufRead).


PHP indenting can be altered in several ways by modifying the values of some

To not enable auto-formating of comments by default (if you want to use your
own 'formatoptions'): >
    :let g:PHP_autoformatcomment = 0

Else, 't' will be removed from the 'formatoptions' string and "qrowcb" will be
added, seefo-tablefor more information.

To add an extra indent to every PHP lines with N being the number of
'shiftwidth' to add: >
    :let g:PHP_default_indenting = N

For example, with N = 1, this will give:
        if (!isset($History_lst_sel))
            if (!isset($History_lst_sel))
                if (!isset($History_lst_sel)) {
                } else

        $command_hist = TRUE;
(Notice the extra indent between the PHP container markers and the code)

To indent PHP tags as the surrounding code: >
    :let g:PHP_outdentphpescape = 0

To automatically remove '\r' characters when the 'fileformat' is set to Unix: >
    :let g:PHP_removeCRwhenUnix = 1

To indent braces at the same level than the code they contain: >
    :let g:PHP_BracesAtCodeLevel = 1
This will give the following result: >
    if ($foo)
Instead of: >
    if ($foo)

NOTE:   Indenting will be a bit slower if this option is used because some
        optimizations won't be available.

To indent 'case:' and 'default:' statements in switch() blocks: >
    :let g:PHP_vintage_case_default_indent = 1

(Since in PHP braces are not required inside 'case/default' blocks, by default they are indented at the same level than the 'switch()' to avoid
unnecessary indentation)

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.

You must also enable filetype indenting (this is activated with the default configuration)

Add this line in your config files:

:filetype indent on

Alternatively you can use a plugin manager (highly recommended) such as Vundle or Pathogen as described below:


  1. Install and configure the Vundle plug-in manager, follow the instructions here
  2. Add the following line to your .vimrc:

     Bundle '2072/PHP-Indenting-for-VIm'
  3. Source your .vimrc with :so % or otherwise reload your VIm
  4. Run the :BundleInstall command


  1. Install the pathogen.vim plug-in, follow the instructions here
  2. Clone the repository under your ~/.vim/bundle/ directory:

     cd ~/.vim/bundle
     git clone git@github.com:2072/PHP-Indenting-for-VIm.git

That's all folks!

Enjoy :)
Return to parent page: Miscellaneous

Valid HTML 4.01! Best viewed in sRGB A utility to annoy spammers Valid CSS!
Previous page - Next page
This page has been seen 14,875 times ; last update: Tue 1 Nov 18:23:52 2016
Copyright 2001 - 2016 John Wellesz '2072'
All rights reserved.
All trademarks and registered trademarks mentioned on this website are the properties of their respective companies.