Take the 2-minute tour ×

When I work on PHP files, there are many chances that I will embed some HTML into it. The problem is when I edit a file named foo.php Vim automatically sets its filetype to php and the snippets for html doensn't work in it.

Well I can quickly set the filetype to html (:set ft=html) but changing between two seems hard to me and I lose the highlightation of php file if I set it to html, its amazing that highlightation for html works if the filetype is php.

I'm using snipMate as my snippet manager. How can I achieve which I want.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You didn't tell us the snippet plugin you're using. For the original snipMate plugin, create a file ~/.vim/ftplugin/php_snipMate.vim with the following contents:

silent call ExtractSnipsFile(g:snippets_dir . 'html.snippets', &l:filetype)

Because of the script name and location, it will be automatically sourced for all PHP files and import the html snippets in addition to all existing PHP snippets.

share|improve this answer
Yes, I am using snipMate. –  Santosh Kumar Sep 18 '12 at 7:26
What if I want to import two snippet files (e.g. css.snippets)? –  Santosh Kumar Sep 18 '12 at 7:46
Repeat the ExtractSnipsFile() call with 'css.snippets'. –  Ingo Karkat Sep 18 '12 at 9:26

It is easy for PHP and HTML condition (and if you use snipMate), I haven't tried on other filetypes. You can set this in you ~/.vimrc file:

autocmd filetype php set filetype=php.html

Point to be noted is when you set the filetype to php.html, the highlightation is for PHP and the snippet will be imported from html. The above configuration will be not same to set ft=html.php (well the snippets will work) because the highlightation will be enabled for html, and as we know php prettifies html but html doesn't prettifies php. In same manner if you do set filetype=php.css.html the snippets for all three filetypes will be imported but the vim will highlight according to PHP.

share|improve this answer
Note that this approach will first source the filetype settings for PHP, then for HTML. In case you have any conflicting mappings / ... in there, you'll get redefinition errors or the later HTML version. The dot-filetypes are rather meant for extensions (such as c.doxygen, not for pulling in alternatives. –  Ingo Karkat Sep 18 '12 at 7:54

Your Answer


