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.
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.
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 offiletype
, butfiletype
affects more things thansyntax
(including syntax highlighting, indenting rules, and plugins), so generally you should usefiletype
unless you only want to affectsyntax
.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 likeperl
,html
,php
, etc.There is another mechanism that can be used to control syntax highlighting called
filetype
, orft
for short. Similar to syntax, you give it a type like this::set filetype=html
. Other filetypes areperl
,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.You can also put this into your .vimrc:
In a .php file (or a html file), you could use a Vim Modeline to force certain commands or settings:
Note that
:set syntax=xml
highlights properly but seems to fail when one is attempting to autoindent the file (i.e. runninggg=G
).When I switched to
:set filetype=xml
, the highlighting worked properly and the file indented properly.