Persistent :set syntax for a given filetype?

一曲冷凌霜 提交于 2019-12-04 07:47:08

问题


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?


回答1:


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.




回答2:


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



回答3:


au BufNewFile,BufRead,BufReadPost *.twig set syntax=HTML

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




回答4:


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



来源:https://stackoverflow.com/questions/11666170/persistent-set-syntax-for-a-given-filetype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!