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
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.
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"
sw
command is because my default is 4, which is too high for HTML.>
).=
.>
(since I have set hlsearch
in my vimrc).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.
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.
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.
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.