Auto format (tabbed layout) .HTML pages with vim
Auto indentation with vim
Or: How do I tidy up an HTML file's indentation in Vi / Vim?
A great little function that vim is able to perform, providing it is enabled in your /etc/vimrc (or .vimrc) config file.
.vimrc config file changes
Just add:
filetype indent on
to /etc/vimrc or .vimrc
Press 'ESC' and type gg=G to auto-format your vim/vi document.
Example html page before auto formatting
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <p>text</p> </body> </html>
Example html page after auto formatting
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <p>text</p> </body> </html>
This function can help you locate missing HTML closing tags
The following example has been auto formatted using gg=G
Because the closing </head> tag is missing, the indentaion is messed up and it's pretty easy to spot where you went wrong.
On a simple page this is trivial, but this really helps out on massive pages.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <body> <p>text</p> </body> </html>
This function works with most programming/ coding languages that vim/ vi 'knows about'.
Vim / Vi has indentation rules for the following coding / programming languages
This function works with most programming/ coding languages that vim/ vi has indentation rules for.
The same languages are also likely to have vim/vi colour syntax highlighting also:
Ant, Automake, Awk, Bib, Bst, Cdl, Changelog, Ch, Cmake, Cobol, Config, Cpp, Css, Cs, C, Dictconf, Dictdconf, Docbk, Dtd, Dtrace, D, Dylan, Eiffel, Erlang, Eruby, Eterm, Fortran, Framescript, GenericIndent, Gitconfig, Haml, Hamster, Htmldjango, Html, Idlang, Ishd, Javascript, Java, Jsp, Ld, Lisp, Logtalk, Lua, Make, Matlab, Mma, Mp, Mupad, Objc, Ocaml, Occam, Pascal, Perl, Php, Postscr, Pov, Prolog, Pyrex, Python, Readline, Rpl, Rst, Ruby, Sass, Scheme, Sdl, Sh, Sml, Sqlanywhere, Sql, Tcl, Tcsh, Tf, Tilde, Vb, Verilog, Vhdl, Xf86conf, Xhtml, Xinetd, Xml, Xsd, Xslt, Yacc, Zsh
An example of Perl auto-indeted using gg=G
sub init($) { my ($self, $args) = @_; unless(keys %list) # already read { local $_; local $/ = "\n"; my $rewind = tell DATA; while(<DATA>) { chomp; next if !length $_ or substr($_, 0, 1) eq '#'; my $os = s/^(\w+)\:// ? qr/$1/i : undef; my ($type, $extensions, $encoding) = split /\;/; next if $args->{only_complete} && ! $extensions; my $extent = $extensions ? [ split /\,/, $extensions ] : undef; my $simplified = MIME::Type->simplified($type); push @{$list{$simplified}}, MIME::Type->new ( type => $type , extensions => $extent , encoding => $encoding , system => $os ); } seek DATA, $rewind, 0; # for forked/mod_perl when badly implemented } $self; }