53

I've been using Vim for a while, and I can't get proper HTML indentation working in PHP files.

For example, what I want is for each child to be indented one tab more than it's parent, as shown below.

<?php
if(isset($sports)) {
    //Do something
?>
<div>
    <label>Uniform Size</label>
    <ul>
        <li class="left"><label for="s" class="small">S</label><input type="radio" name="size[]" value="S" id="s" class="radio" /></li>
        <li class="left"><label for="m" class="small">M</label><input type="radio" name="size[]" value="M" id="m" class="radio" /></li>
        <li class="left"><label for="l" class="small">L</label><input type="radio" name="size[]" value="L" id="l" class="radio" /></li>
        <li class="left"><label for="xl" class="small">XL</label><input type="radio" name="size[]" value="XL" id="xl" class="radio" /></li>
    </ul>
</div>
<?php
}
?>

Using the PHP-correct-Indent script, the code results in being formatted as follows:

<?php
if(isset($sports)) {
    //Do something
?>
<div>
<label>Uniform Size</label>
<ul>
<li class="left"><label for="s" class="small">S</label><input type="radio" name="size[]" value="S" id="s" class="radio" /></li>
<li class="left"><label for="m" class="small">M</label><input type="radio" name="size[]" value="M" id="m" class="radio" /></li>
<li class="left"><label for="l" class="small">L</label><input type="radio" name="size[]" value="L" id="l" class="radio" /></li>
<li class="left"><label for="xl" class="small">XL</label><input type="radio" name="size[]" value="XL" id="xl" class="radio" /></li>
</ul>
</div>
<?php
}
?>

Even with indented HTML which I then add PHP code to, the indentation is ignored, moving new lines of HTML code without any indentation at all.

So, is there any way that I can get the indentation format that I want working with HTML within PHP files, using Vim?

9 Answers 9

67

This still bothers me. I only just decided that the best work-around (for me personally) is this:

:set filetype=html

And then highlight your text and hit =. BOOM! HTML formatting succes. (Not ideal, I know, but at least it works.)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for this. After a point I find it easier to do this sort of thing than to fight automatic workarounds.
After more than a year of using Vim I've changed my perspective. Toggling between :set ft=php and :set ft=html is a better solution to the problem.
To get better php support you might try :set ft=phtml
@JoeMills that sounds good I'll try that the next time I'm cursed with writing php. ;)
To make this a little less painful, add this to your vimrc: map <leader>h :set ft=html<CR> and map <leader>p :set ft=php<CR>
|
27

There is a set of vimrc instructions on the Vim Wiki called Better indent support for PHP with HTML that will use the correct plugin depending on the block.

There is also a Vundle/Pathogen Plugin that uses the same code but is easier to install and keeps your .vimrc clean.

Pathogen

cd ~/.vim/bundle
git clone https://github.com/captbaritone/better-indent-support-for-php-with-html.git

Vundle

Place in .vimrc

Bundle 'captbaritone/better-indent-support-for-php-with-html'

Run in vim

:BundleInstall

6 Comments

This also works under Cygwin - the file should be located at ~/.vim/indent/php.vim. If you paste from that page into Cygwin, double-check that the pasted code wasn't screwed up (wrong line breaks and extraneous quotation marks) in the process.
Thanks Brian. That seems to be working almost exactly as I want it to. Thank you Naum and bobince as well for your answers.
I followed the tip, but it's not working for me. Do I have to explicit call the function or it's supposed to work out of the box? if I have to call it, what parameter should I pass?
If you're using pathogen throw this file in your autoload/php/indent folder with the name php.vim and it should work fine. If on MacVim, either reload or restart MacVim.
This worked perfectly for me. I wrapped it in a Vundle compatible plugin to make it easier to include: github.com/captbaritone/better-indent-support-for-php-with-html
|
19

After looking really really hard into all solutions, I found out this plugin:

http://www.vim.org/scripts/script.php?script_id=604

It seems to have solved my problems!!!!!

4 Comments

Instructions are lacking, put this in the ~/.vim/indents folder
this screwed up indentation in my php code files... somehow set the tabstop to 3 spaces. Not worth it, IMHO
@mblackwell8 You can edit the php.vim file, line 27, to control indent spacing. You can set it to the number of spaces you prefer or if rather have tabs, just delete the line.
This is new php 5.4 syntax, try it vim.org/scripts/script.php?script_id=4159
14

For me it works good if I first do :set ft=html and then :set syn=php.

3 Comments

I'm using this solution. The only inconvenient is that language support (coc-phpls) stops working.
This should really be the correct answer. No plugins or scripts, just vanilla vim settings.
Nice! ft=html fixes indenting but messes up the php syntax highlighting, and syn=php fixes it back.
6

In php+html I found the following is good for me.

:set ft=html # Change the file type to html
=G # to indent all lines 
:set ft=phtml # Change the file type to phtml
=G # to indent all php lines

2 Comments

that's really help me a lot. Why this shouldn't be the answer then?
It indent only current line and all lines bellow to the end of the file.
2

php-correct-indenting only cares about your PHP, and assumes the readability of the HTML is of no interest. An XML indenter would position the tags nicely, but wouldn't be able to indent the contents of a <?php> processing instruction to match. Maybe there is an indentation script that understands both the C-like syntax of PHP the programming language and [X][HT]ML the markup language being templated, but I've never met one yet - sorry.

Still, I'd like to fiddle with the indenting in your example even before php-correct-indenting mauled it! The <div> element is inside an outer if-statement, but I have no way to see that from the indenting. I'd suggest something like:

<?php if(isset($sports)) { ?>
    <?php
        // Do something
    ?>
    <div>
        <label>Uniform Size</label>
        <ul>
            <li>etc. etc.</li>
        </ul>
    </div>
<?php } ?>

Comments

1

i found this solution is much better. http://www.vim.org/scripts/script.php?script_id=1120

supporting HEREDOC html style. which occur frequently in my code.
BTW:it's has more versions than the old one (script id 604, alex posted it above)

Comments

1

inside your .vimrc:

:function IndentPHPHTML()
:  set ft=html
:  normal gg=G
:  set ft=php
:endfunction

use ctrl-shift-L (or whatever) to indent

nnoremap <C-S-l> :call IndentPHPHTML()<cr>

Comments

0

After searching for days for the solution ,nothing worked and finally this worked,add this to your vimrc

au BufEnter,BufNew *.php :set filetype=html

1 Comment

this will ruin the syntax highlighting and indenthing for php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.