使用 Vim 正确缩进 HTML 和 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/459478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:49:52  来源:igfitidea点击:

Correct indentation of HTML and PHP using Vim

phphtmlvimindentation

提问by Sasha

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

我已经使用 Vim 一段时间了,但我无法在 PHP 文件中使用正确的 HTML 缩进。

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-Indentscript, the code results in being formatted as follows:

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

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.

即使使用缩进的 HTML,然后我将 PHP 代码添加到其中,缩进也会被忽略,移动新的 HTML 代码行而根本没有任何缩进。

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

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

回答by steve

This still bothers me. I only justdecided 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.)

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

回答by Brian Carper

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

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

There is also a Vundle/Pathogen Pluginthat uses the same code but is easier to install and keeps your .vimrcclean.

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

Pathogen

病原

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

Vundle

文德尔

Place in .vimrc

放入 .vimrc

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

Run in vim

在 vim 中运行

:BundleInstall

回答by alessioalex

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

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

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

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

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

好像解决了我的问题!!!!!

回答by gitaarik

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

对我来说,如果我先做:set ft=html然后:set syn=php.

回答by shin

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

在 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

回答by xinquan

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

我发现这个解决方案要好得多。 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)

支持 HEREDOC html 样式。这在我的代码中经常发生。
顺便说一句:它的版本比旧版本多(脚本 ID 604,alex 在上面发布了)

回答by bobince

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.

php-correct-indenting 只关心你的 PHP,并假设 HTML 的可读性无关紧要。XML 缩进器可以很好地定位标签,但不能缩进 <?php> 处理指令的内容以进行匹配。也许有一个缩进脚本可以理解 PHP 编程语言的类 C 语法和 [X][HT]ML 被模板化的标记语言,但我还没有遇到过 - 抱歉。

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-correct-indenting 破坏它之前摆弄您示例中的缩进!<div> 元素位于外部 if 语句内,但我无法从缩进中看出这一点。我建议如下:

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

回答by Ricardo Schalch

inside your .vimrc:

在你的 .vimrc 中:

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

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

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

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

回答by kapil

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

在搜索了几天的解决方案后,没有任何效果,最后这有效,将其添加到您的 vimrc

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

回答by Naum

In your ~/.vimrcfile:

在您的~/.vimrc文件中:

set expandtab
set sw=4
set ts=4

The expandtaboption will convert tabs to spaces, the swoption sets your shift width to 4 and the tssets tab stop to 4 spacs.

expandtab选项会将制表符转换为空格,该sw选项将您的移位宽度设置为 4,将制表位ts设置为 4 个空格。