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

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

提交回复
热议问题