Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change filetype (or other Vim settings) blockwise in a file?

I program a lot of Perl in Vim. Often I just hack together some CGI script and put the CSS right into the program code. So lately I asked myself if it was possible, to put some Vim-specific comments around such blocks in my code, so that vim highlights the specific area not as a Perl script, but as a cascading stylesheet.

This also would be neat when working with Mojolicious where you can embed entire templates just into the DATA-area but lose all the highlighting of the HTML then.

Of course, I could switch manually between the filetypes/highlighting. But I wonder if there is better way.

Thanks, Sven

like image 243
Sven Eppler Avatar asked Jul 28 '11 19:07

Sven Eppler


2 Answers

You can have multiple filetypes set for a single file :

:set ft=perl.css

With that you get omnicompletion (and snippets if applicable) for both languages and semi-correct highlighting: if you put your CSS rules in quotes it will be highlighted as a string.

I've just found (yesterday) a cool plugin inspired by an Emacs feature called NrrwRgn. It allows you to select a "region" of code, say the CSS part of your Perl file and edit it in a scratch window for which you :set ft=css. Each save is reflected in the original window. Very useful when dealing with PHP templates full of PHP/HTML/JS/CSS.

like image 186
romainl Avatar answered Sep 20 '22 12:09

romainl


If you don't want to come up with your own hybrid highlighting rules:

:set syntax=perl

then when you want to edit css,

:set syntax=css

To make it easier, you could map some keys for both in your .vimrc, which would make it easy to toggle back and forth.

map <F3> :execute "set syntax=perl" <CR>
map <F4> :execute "set syntax=css" <CR>
like image 33
tester Avatar answered Sep 20 '22 12:09

tester