Correct indentation of HTML and PHP using Vim

前端 未结 10 434
悲&欢浪女
悲&欢浪女 2020-11-29 20:02

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 mo

相关标签:
10条回答
  • 2020-11-29 20:14

    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:

    <?php if(isset($sports)) { ?>
        <?php
            // Do something
        ?>
        <div>
            <label>Uniform Size</label>
            <ul>
                <li>etc. etc.</li>
            </ul>
        </div>
    <?php } ?>
    
    0 讨论(0)
  • 2020-11-29 20:14

    In your ~/.vimrc file:

    set expandtab
    set sw=4
    set ts=4
    

    The expandtab option will convert tabs to spaces, the sw option sets your shift width to 4 and the ts sets tab stop to 4 spacs.

    0 讨论(0)
  • 2020-11-29 20:20

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

    0 讨论(0)
  • 2020-11-29 20:20

    inside your .vimrc:

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

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

    nnoremap <C-S-l> :call IndentPHPHTML()<cr>
    
    0 讨论(0)
  • 2020-11-29 20:29

    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!!!!!

    0 讨论(0)
  • 2020-11-29 20:29

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

    :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
    
    0 讨论(0)
提交回复
热议问题