117

I'm working on a Symfony2 project which uses Twig, and the filetypes are myfile.html.twig. Vim doesn't automatically detect the syntax highlighting and so applies none. I can use :set syntax=HTML after I've opened the file but this is a pain when jumping between files.

Is there a way to persistently set the syntax highlighting for a specific file type in vim?

Micha Wiedenmann
  • 17,330
  • 20
  • 79
  • 123
Bendihossan
  • 2,099
  • 5
  • 18
  • 25

4 Answers4

127

You can use autocmd to accomplish that, i.e.:

augroup twig_ft
  au!
  autocmd BufNewFile,BufRead *.html.twig   set syntax=html
augroup END

Should work.

Hauleth
  • 20,457
  • 4
  • 58
  • 98
  • 4
    A friend just pointed this out to me as well. Set the filetype: `au BufRead,BufNewFile *.html.twig set filetype=twig` Set the syntax: `au BufRead,BufNewFile *.html.twig set syntax=HTML` – Bendihossan Jul 26 '12 at 09:27
  • 34
    I would suggest to put this line to `~/.vim/ftdetect/html.twig.vim` file (you should create it), which is the right place for such autocommands. – xaizek Jul 26 '12 at 09:30
  • 1
    @xaizek I understand this is recommended, but do you have any idea why it does not work for me if I put in `~/.vimrc` instead of `~/.vim/ftdetect/`? – Haralan Dobrev May 26 '14 at 23:56
  • 3
    @HaralanDobrev, it probably gets overwritten by another autocommand defined after this line. Run `:autocmd BufNewFile,BufRead *.html.twig` inside Vim to see all registered autocommands (also try with separate events, only `BufNewFile` and only `BufRead`). Also check output of `:verbose set syntax?`. – xaizek May 27 '14 at 18:50
  • 1
    How easy it was to find this solution makes me wish that the vim wiki didn't exist – Jared Beach Apr 07 '16 at 15:09
79

Add one of the following passages to your .vimrc:

" Set the filetype based on the file's extension, overriding any
" 'filetype' that has already been set
au BufRead,BufNewFile *.html.twig set filetype=html

or

" Set the filetype based on the file's extension, but only if
" 'filetype' has not already been set
au BufRead,BufNewFile *.html.twig setfiletype html
bdesham
  • 13,980
  • 10
  • 70
  • 116
Muhammad Reda
  • 24,289
  • 12
  • 85
  • 99
  • 8
    This is a better generic answer than the accepted one. If vim cannot detect the type of file (and automatically provide syntax highlighting), it is better to set the filetype rather than just the syntax of the file. – mrfred Jun 13 '17 at 18:48
  • 2
    `setfiletype` should be `set filetype` At least that's how I could make it work – dau_sama Aug 17 '17 at 15:07
  • 1
    @dau_sama `setfiletype=html` is not valid syntax; I’ve fixed the answer. **Note** that `setfiletype html` and `set filetype=html` do different things, as mentioned in the answer. – bdesham Jun 26 '18 at 17:16
11
au BufNewFile,BufRead,BufReadPost *.twig set syntax=HTML

And add this line to ~/.vimrc to make the settings persistent.

lucapette
  • 19,852
  • 5
  • 63
  • 59
Igor Chubin
  • 51,940
  • 8
  • 108
  • 128
3

I know this doesn't directly answer the question, however this answers the intent of the question, which is to get syntax highlighting working with Twig / Symfony 2

I suggest you check out https://github.com/beyondwords/vim-twig (not mine), which provides:

  • the syntax highlighting file for *.html.twig,
  • file type detection for same, and
  • file type plugin, allowing you to modify various settings as required when editing *.html.twig files

I hope this helps

ebonhand
  • 346
  • 1
  • 8