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

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

提交回复
热议问题