Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct indentation of HTML and PHP using Vim

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?

like image 209
Sasha Avatar asked Jan 19 '09 22:01

Sasha


People also ask

What is proper indentation for HTML?

Indent by two spaces per indentation level. Use all-lowercase for elements and attributes. Don't leave trailing spaces at the end of a line (except as needed for Markdown).

How do I set an indent in Vim?

To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . ) then try hitting the F5 key while in insert mode (or just :set paste ).

How do I indent PHP code?

Spacing and indentation should be consistent throughout your code. Many developers choose to use 4-space or 2-space indentation. In PHP, each nested statement (e.g., a statement following a "{" brace) should be indented exactly once more than the previous line's indentation.


2 Answers

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

like image 175
steve Avatar answered Sep 19 '22 15:09

steve


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 
like image 36
Brian Carper Avatar answered Sep 20 '22 15:09

Brian Carper