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

后端 未结 11 1745
庸人自扰
庸人自扰 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:48

    I tried the usual "gg=G" command, which is what I use to fix the indentation of code files. However, it didn't seem to work right on HTML files. It simply removed all the formatting.

    If vim's autoformat/indent gg=G seems to be "broken" (such as left indenting every line), most likely the indent plugin is not enabled/loaded. It should really give an error message instead of just doing bad indenting, otherwise users just think the autoformat/indenting feature is awful, when it actually is pretty good.

    To check if the indent plugin is enabled/loaded, run :scriptnames. See if .../indent/html.vim is in the list. If not, then that means the plugin is not loaded. In that case, add this line to ~/.vimrc:

    filetype plugin indent on
    

    Now if you open the file and run :scriptnames, you should see .../indent/html.vim. Then run gg=G, which should do the correct autoformat/indent now. (Although it won't add newlines, so if all the html code is on a single line, it won't be indented).

    Note: if you are running :filetype plugin indent on on the vim command line instead of ~/.vimrc, you must re-open the file :e.

    Also, you don't need to worry about autoindent and smartindent settings, they are not relevant for this.

    0 讨论(0)
  • 2020-11-29 15:49

    This is my solution that works nicely for opening "ugly" HTML in a nicely spaced way:

    vim fileIn.html -c "set sw=2 | %s/>/>\r/ | execute 'normal gg=G' | set nohlsearch | g/^\\s*\$/d"
    
    • The sw command is because my default is 4, which is too high for HTML.
    • The next part adds a newline (Vim thinks it's a carriage return, sigh) after each element (>).
    • Then re-indent the entire file with =.
    • Then unhighlight > (since I have set hlsearch in my vimrc).
    • Then remove all empty/whitespace-only lines (see "Vim delete blank lines" for more, also this is double-escaped because it's in the shell).

    You can even add | wq! fileOut.html to the end if you don't want to enter Vim at all, but just clean up the file.

    0 讨论(0)
  • 2020-11-29 15:49

    None of the answers worked for me because all my HTML was in a single line.

    Basically you need first to break each line with the following command that substitutes >< with the same characters but with a line break in the middle.

    :%s/></>\r</g
    

    Then the command

    gg=G
    

    will indent the file.

    0 讨论(0)
  • 2020-11-29 15:57

    As tylerl explains above, set the following:

    :filetype indent on
    :set filetype=html
    :set smartindent
    

    However, note that in vim 7.4 the HTML tags html, head, body, and some others are not indented by default. This makes sense, as nearly all content in an HTML file falls under those tags. If you really want to, you can get those tags to be indented like so:

    :let g:html_indent_inctags = "html,body,head,tbody" 
    

    See "HTML indenting not working in compiled Vim 7.4, any ideas?" and "alternative html indent script" for more information.

    0 讨论(0)
  • 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 <leader>tidy :%! tidy -config ~/vimfiles/ftplugin/tidyrc_html.txt <CR>
    

    To use it, just open an HTML file, and type /tidy. (That / is the <leader> 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.

    0 讨论(0)
提交回复
热议问题