Fixed HTML and PHP selection using Vim

I have been using Vim for a while, and I cannot get the correct HTML indentation working in PHP files.

For example, I want each tab to be indented one tab more than the 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 will be 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 HTML indentation, which I then add to the PHP code, the indentation is ignored, moving new lines of HTML code without any indentation.

So, is there a way to get the indentation format that I want to work with HTML in PHP files using Vim?

+43
html vim php indentation
Jan 19 '09 at 22:39
source share
10 answers

It still bothers me. I just decided that the best job (for me personally) is this:

 :set filetype=html 

Then highlight the text and press = . BOOM! HTML formatting is successful. (Not perfect, I know, but at least it works.)

+49
Nov 03 '10 at 7:49
source share

There is a set of vimrc instructions on the Vim Wiki called Best indentation support for PHP with HTML , which will use the correct plugin depending on the block.

There is also a Vundle / Pathogen Plugin that uses the same code, but it 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' 

Running in vim

 :BundleInstall 
+19
Jan 20 '09 at 18:44
source share

After I really looked at all the solutions, I recognized this plugin:

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

I think I solved my problems !!!!!

+19
May 13 '11 at 20:31
source share

In php + html, I found the following for me well.

 :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 
+5
Jan 07 '14 at 13:05
source share

For me, this works well if I first do :set ft=html and then :set syn=php .

+5
Nov 19 '14 at 12:45
source share

php-correct-indenting only cares about your PHP and assumes that HTML readability is not of interest. The XML indenter positions the tags nicely, but cannot retreat from the contents of the <php> processing instruction. Perhaps there is an indented script that understands both the C-type syntax of PHP, the programming language, and the [X] [HT] ML, markup template language, but I have never met it yet - sorry.

However, I would like to mess with the indentation in your example, even before php-correct-indenting worn it out! The <div> element is inside the external if statement, but I have no way to see this from the indent. I would suggest something like:

 <?php if(isset($sports)) { ?> <?php // Do something ?> <div> <label>Uniform Size</label> <ul> <li>etc. etc.</li> </ul> </div> <?php } ?> 
+1
Jan 20 '09 at 1:51
source share

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

supports the HEREDOC html style. which are often found in my code.
BTW: it has more versions than the old one (script id 604, alex sent it higher)

+1
Sep 21 '11 at 3:10
source share

inside your .vimrc:

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

use ctrl-shift-L (or something else) to indent

 nnoremap <CSl> :call IndentPHPHTML()<cr> 
0
Mar 28 '16 at 16:05
source share

After searching for days to solve, nothing worked and finally it worked, add this to your vimrc

 au BufEnter,BufNew *.php :set filetype=html 
0
Dec 30 '16 at 2:47
source share

In your ~/.vimrc file:

 set expandtab set sw=4 set ts=4 

The expandtab option converts tabs to spaces, the sw parameter sets the shift width to 4, and the ts tab sets the value to 4 spaces.

-7
Jan 20 '09 at 0:47
source share



All Articles