181

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Karthick
  • 4,456
  • 7
  • 28
  • 34

7 Answers7

292
:set syntax=html
Amber
  • 507,862
  • 82
  • 626
  • 550
143

You can also put this into your .vimrc:

au BufReadPost *.ezt set syntax=html
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Benoit
  • 76,634
  • 23
  • 210
  • 236
  • E216: No such group or event: BufLoad *.ezt set syntax=html ... what am i doing wrong? – Karthick Oct 10 '10 at 09:38
  • 2
    In my experience this approach will fail if you give an ***exact*** filename e.g. `.myspecialdotfile`. It needs a star in it somewhere to work, even if it's theoretically not necessary. – Seldom 'Where's Monica' Needy Oct 15 '16 at 05:18
  • 1
    I have this in my `.vimrc` file for Dockerfiles that have extensions for different purposes. autocmd BufNewFile,BufRead Dockerfile\* set filetype=docker Just using `Dockerfile*` prevents syntax highlighting from working when using `vi Dockerfile` - adding the backslash allows it to work for `Dockerfile` / `Dockerfile.something` and `Dockerfilesomething` – Android Control Oct 15 '19 at 12:22
34

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.

icc97
  • 11,395
  • 8
  • 76
  • 90
slm
  • 15,396
  • 12
  • 109
  • 124
31

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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
wisbucky
  • 33,218
  • 10
  • 150
  • 101
21

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.

icc97
  • 11,395
  • 8
  • 76
  • 90
Daniel
  • 855
  • 7
  • 9
19

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()
 ~
 ~
  • 2
    You mean Modeline – Shammel Lee May 11 '17 at 19:47
  • 1
    Sometimes the syntax-related `au ...` settings in my `~/.vimrc` get ignored for certain files (presumably due to modified metadata?). Other than copying that file content to a fresh file, setting a modeline at the top of that file works. E.g., for a BASH script, adding a second line, `# vim: syntax=sh` after the shebang (first line: `#!/bin/bash`) seems to work, reliably. Since it's basically a "comment", I just include both lines in my `sbb` ("shebang bash") snippet. – Victoria Stuart May 02 '18 at 22:41
  • Yes, having something like `# vim:ft=dosini` or `# vim:ft=sh` as the first line works for me as well. Especially useful if it's a config file with no file extension. – Alex Vanu Mar 09 '23 at 09:59
2

What worked for me is:

:syntax on
JorgeMadson
  • 140
  • 10