使用 Vim 正确缩进 HTML 和 PHP
我已经使用 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 的缩进格式吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这仍然困扰着我。我只是刚刚决定最好的解决方法(对我个人而言)是这样的:
然后突出显示您的文本并点击
=
。繁荣! HTML 格式化成功。 (我知道这并不理想,但至少它有效。)This still bothers me. I only just decided that the best work-around (for me personally) is this:
And then highlight your text and hit
=
. BOOM! HTML formatting succes. (Not ideal, I know, but at least it works.)Vim Wiki 上有一组 vimrc 指令,名为 Better indent support for PHP with HTML,它将根据块使用正确的插件。
还有一个 Vundle/Pathogen 插件,它使用相同的代码,但更容易安装并保持
.vimrc
干净。Pathogen
Vundle
放入 .vimrc
在 vim 中运行
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
Vundle
Place in .vimrc
Run in vim
在认真研究了所有解决方案之后,我发现了这个插件:
http:// www.vim.org/scripts/script.php?script_id=604
似乎解决了我的问题!!!!
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!!!!!
对我来说,如果我先执行
:set ft=html
然后执行:set syn=php
,效果会很好。For me it works good if I first do
:set ft=html
and then:set syn=php
.在 php+html 中,我发现以下内容对我有好处。
In php+html I found the following is good for me.
php- Correct-indenting 只关心你的 PHP,并假设 HTML 的可读性不重要。 XML 缩进器可以很好地定位标签,但无法缩进 的内容。处理指令进行匹配。也许有一个缩进脚本可以理解 PHP 编程语言的类 C 语法和 [X][HT]ML 模板化的标记语言,但我还没有遇到过 - 抱歉。
尽管如此,即使在 php- Correct-indenting 破坏它之前,我还是想摆弄你的示例中的缩进!
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:
我发现这个解决方案要好得多。
http://www.vim.org/scripts/script.php?script_id=1120
支持 HEREDOC html 样式。这在我的代码中经常出现。
顺便说一句:它比旧版本有更多版本(脚本 ID 604,alex 在上面发布)
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)
在你的 .vimrc 中:
使用 ctrl-shift-L (或其他)来缩进
inside your .vimrc:
use ctrl-shift-L (or whatever) to indent
经过几天的寻找解决方案后,没有任何效果,最后这成功了,将其添加到您的 vimrc
After searching for days for the solution ,nothing worked and finally this worked,add this to your vimrc