使用Vim正确缩进HTML和PHP的缩进

时间:2009-01-19 22:39:49

标签: php html vim indentation

我一直在使用Vim,我无法在PHP文件中使用正确的HTML缩进。

例如,我想要的是每个孩子缩进一个标签而不是父母,如下所示。

<?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
}
?>

使用PHP-correct-Indent脚本,代码的格式如下:

<?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
}
?>

即使使用缩进的HTML然后我添加PHP代码,缩进也会被忽略,移动新的HTML代码行而不会有任何缩进。

那么,有没有什么方法可以使用Vim获得我想在PHP文件中使用HTML的缩进格式?

10 个答案:

答案 0 :(得分:53)

这仍然困扰着我。我只是只是决定最好的解决方法(对我个人而言)是这样的:

:set filetype=html

然后突出显示您的文字并点击=。繁荣! HTML格式化成功。 (不太理想,我知道,但至少它有效。)

答案 1 :(得分:20)

在Vim Wiki上有一组名为Better indent support for PHP with HTML的vimrc指令,它们将根据块使用正确的插件。

还有Vundle/Pathogen Plugin使用相同的代码,但更容易安装并保持.vimrc清洁。

<强>病原

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

<强> Vundle

放入.vimrc

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

在vim中运行

:BundleInstall

答案 2 :(得分:19)

在仔细研究所有解决方案后,我发现了这个插件:

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

似乎已经解决了我的问题!!!!!

答案 3 :(得分:9)

对我来说,如果我先:set ft=html然后:set syn=php,那就行得很好。

答案 4 :(得分:5)

在php + html中,我发现以下内容对我有好处。

: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 :(得分:1)

php-correct-indenting只关心你的PHP,并假设HTML的可读性没有意义。 XML压头会很好地定位标签,但是无法缩进&lt;?php&gt;的内容。处理指令匹配。也许有一个缩进脚本,既可以理解PHP编程语言的C语法,也可以理解[X] [HT] ML标记语言的模板化,但我还没有遇到过 - 抱歉。

尽管如此,即使在php-correct-indenting mauled之前,我还是想在你的例子中进行缩进! &lt; div&gt; element位于外部if语句中,但我无法从缩进中看到它。我建议像:

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

答案 6 :(得分:1)

我发现这个解决方案要好得多。 http://www.vim.org/scripts/script.php?script_id=1120

支持HEREDOC html风格。这在我的代码中经常出现 顺便说一句:它的版本比旧的版本多(脚本编号604,亚历克斯贴在上面)

答案 7 :(得分:0)

在.vimrc中:

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

使用ctrl-shift-L(或其他)缩进

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

答案 8 :(得分:0)

在搜索解决方案的天数后,没有任何工作,最后这个工作,将其添加到您的vimrc

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

答案 9 :(得分:-8)

~/.vimrc文件中:

set expandtab
set sw=4
set ts=4

expandtab选项会将标签转换为空格,sw选项会将您的转移宽度设置为4,ts设置标签设置为4个空格。