Home page

767,963 Visitors, Monday 14 October 2024 15:33:20 CEST, IP: 35.173.48.18

Return to parent page: Miscellaneous

PHP Indentation VIm script

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


Download here (version 1.75 released on 2023-08-08 - a complete changelog is available)


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

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

<?php
if (!isset($History_lst_sel))
    if (!isset($History_lst_sel))
        if (!isset($History_lst_sel)) {
            $History_lst_sel=0;
        } else
            $foo="truc";
$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:


<?php
if ($foo="thing") // as you see an opened bracket isn't needed to indent correctly
    if (foo("something"))
    {
        somethingelse("blabla");
?>


<html>
<head>
<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) {
            nothing();
        }

        </script>

</head>
<body>

<b>some closing and oppenning bracket here } } {
nothing to indent here!
</b>

</body>
</html>


<?
        Somethingelse(true);
    }

if (foo)
    /* a useless comment here
    with useless code:
    if (broken code) {
                   blabla();
    */
    if (testHereDocument()) {


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

    }

bye();
?>


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).

OPTIONS:

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

                                                                php-comment
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:
>
    <?php
        if (!isset($History_lst_sel))
            if (!isset($History_lst_sel))
                if (!isset($History_lst_sel)) {
                    $History_lst_sel=0;
                } else
                    $foo="bar";

        $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)
        {
        foo();
        }
Instead of: >
    if ($foo)
    {
        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:

Vundle

  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

Pathogen

  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 https://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 Valid CSS!
Previous page - Next page
This page has been seen 26,056 times ; last update: Mon Sep 4 14:44:20 2023
Copyright © 2001 - 2024 John Wellesz
All rights reserved.
All trademarks and registered trademarks mentioned on this website are the properties of their respective companies.

Privacy Policy