Correct Indentation of HTML and PHP Using Vim

Correct indentation of HTML and PHP using Vim

This still bothers me. I only just decided that the best work-around (for me personally) is this:

:set filetype=html

And then highlight your text and hit =. BOOM! HTML formatting succes. (Not ideal, I know, but at least it works.)

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

With filetype indent on inside my .vimrc, Vim indents HTML files quite nicely.

Simple example with a shiftwidth of 2:

<html>
<body>
<p>
text
</p>
</body>
</html>

How do i fix the indents in my html/php files in vim?

Make sure both of the following are set in your .vimrc file

set tabstop=4
set shiftwidth=4

Making Vim auto-indent PHP/HTML using alternative syntax

It would seem that this is not possible given the currently available Vim plugins, nor is it likely to be addressed.

Trying to set .html files with htmljinja filetype and get correct indentation

In the meantime i circumvent vim's indentation capabilities and rather rely on specific indenters/reformatter wherever i can (by the way if anybody is aware of a tex/latex reformatter please let me know).

As a result the following lines found their way into my .vimrc:

au FileType c,cpp let &l:equalprg="indent -br -l62 -nce -blf -bbb -i4 -nbfde -nbfda -bad -bap -cli4 -nut"
au FileType perl let &l:equalprg="perltidy"
noremap <leader>i mzgg=G`z

A drawback of this approach is that you can't enjoy automatic indentation while writing. Nevertheless I get used to it very fast and I think of it as a very solid and suitable solution to the indentation-problem. Moreover the indented code looks great and i doubt that those fancy IDE's keep up with it.

Now, what has this got to do with your problem?

Following my approach i searched for an html-reformatter and found tidy. tidy seems to be a monster, that not only can pretty-print your html files but also can validate and correct them.

In order to cope with tidy you have to write a config file for your indentation purposes. The following one works for me:

~/.tidyrc_indent:

indent: auto
indent-spaces: 3
show-warnings: no
show-errors: 0
quiet: yes
indent-cdata: yes
output-html: yes
wrap: 80

But make sure to read the man pages of tidy because the options seems to be endless.

Now you can add

au FileType htmljinja let &equalprg="tidy -config ~/.tidyrc"

to your .vimrc and start indenting your html file with gg=G or a derived mapping that needs less keystrokes.

Please take care as tidy like any other reformatter may mess up your code. Therefore you should back up your code regularly (maybe with git or something similar). Although i never experienced this by myself wiht perltidy and indent there are some reports on the internet of angry users that complain about such events.

How can i indent my html file with vim correctly?

It works out of the box for me. Try if it works with the minimal setup:

$ vim -u NONE
:set nocp
:filetype indent on
:set ft=html

and type in some HTML code.

The second tag should be indented by a tab now.

How do I fix the indentation of an entire file in Vi?

=, the indent command can take motions. So, gg to get the start of the file, = to indent, G to the end of the file, gg=G.



Related Topics



Leave a reply



Submit