Karthick
Karthick

Reputation: 4776

How to force Vim to syntax-highlight a file as HTML?

How do I set Vim's syntax highlighting to treat a file extension as an HTML file?

I'm using ez template, so the file's extension is .ezt. But a lot of it is normal HTML code.

Upvotes: 205

Views: 156437

Answers (7)

JorgeMadson
JorgeMadson

Reputation: 158

What worked for me was:

:syntax on

Upvotes: 3

wisbucky
wisbucky

Reputation: 38083

To make it automatic, add this line to your ~/.vimrc:

autocmd BufNewFile,BufRead *.ezt set filetype=html

If you want to just do it for the current file, then type:

:set filetype=html

You could also substitute syntax instead of filetype, but filetype affects more things than syntax (including syntax highlighting, indenting rules, and plugins), so generally you should use filetype unless you only want to affect syntax.

Upvotes: 43

In a .php file (or a html file), you could use a Vim Modeline to force certain commands or settings:

 1 /* vim: syntax=javascript
 2  *
 3  * .submit_norefresh()
 ~
 ~

Upvotes: 21

Benoit
Benoit

Reputation: 79243

You can also put this into your .vimrc:

au BufReadPost *.ezt set syntax=html

Upvotes: 145

Daniel
Daniel

Reputation: 895

Note that :set syntax=xml highlights properly but seems to fail when one is attempting to autoindent the file (i.e. running gg=G).

When I switched to :set filetype=xml, the highlighting worked properly and the file indented properly.

Upvotes: 22

slm
slm

Reputation: 16456

Take a look at this Vim wikia topic. Some useful tips:

  • As other answers have mentioned, you can use the vim set command to set syntax. :set syntax=<type> where <type> is something like perl, html, php, etc.

  • There is another mechanism that can be used to control syntax highlighting called filetype, or ft for short. Similar to syntax, you give it a type like this: :set filetype=html. Other filetypes are perl, php, etc.

  • Sometimes vim "forgets" what syntax to use, especially if you're mixing things like php and html together. Use the keyboard shortcut Ctrl+L (<C-L>) to get vim to refresh the highlighting.

Upvotes: 37

Amber
Amber

Reputation: 527548

:set syntax=html

Upvotes: 323

Related Questions