Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable snippet for HTML in PHP files?

Tags:

vim

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.

like image 577
Santosh Kumar Avatar asked Feb 19 '23 03:02

Santosh Kumar


2 Answers

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.

like image 197
Santosh Kumar Avatar answered Feb 24 '23 16:02

Santosh Kumar


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.

like image 34
Ingo Karkat Avatar answered Feb 24 '23 16:02

Ingo Karkat