How do I tidy up an HTML file's indentation in VI?

后端 未结 11 1897
庸人自扰
庸人自扰 2020-11-29 15:02

How do I fix the indentation of his huge html files which was all messed up?

I tried the usual \"gg=G command, which is what I use to fix the indentation of code fil

11条回答
  •  鱼传尺愫
    2020-11-29 15:57

    Here's a heavy-weight solution that gets you indenting, plus all the HTML pretty-printing you don't necessarily want to care about while you're editing.

    First, download Tidy. Make sure you add the binary to your path, so you can call it from any location.

    Next, create a config file describing your favorite HTML flavor. Documentation is not great for Tidy, but here's an overview, and a list of all the options. Here's my config file:

    bare: yes
    break-before-br: no
    clean: yes
    drop-proprietary-attributes: yes
    fix-uri: yes
    indent-spaces: 4
    indent: yes
    logical-emphasis: yes
    markup: yes
    output-xhtml: yes
    quiet: yes
    quote-marks: yes
    replace-color: yes
    tab-size: 4
    uppercase-tags: no
    vertical-space: yes
    word-2000: yes
    wrap: 0
    

    Save this as tidyrc_html.txt in your ftplugin folder (under vimfiles).

    One more file: add the following line to (or create) html.vim, also in ftplugin:

    map tidy :%! tidy -config ~/vimfiles/ftplugin/tidyrc_html.txt 
    

    To use it, just open an HTML file, and type /tidy. (That / is the key.)

    There you go! Not a quick solution, by any means, but now you're a bit better equipped for editing those huge, messed-up HTML files.

提交回复
热议问题