Set colorscheme based on filetype

535 views
Skip to first unread message

Kristoffer Stensbo-Smidt

unread,
Aug 6, 2008, 12:26:03 PM8/6/08
to vim_use
I would like Vim to change the color scheme according to the file type
being edited. For example, I would like to use the color scheme
mycolors1 for Python editing and mycolors2 for everything else. How do
I achieve this?

Best regards,
Kristoffer

Michael Wagner

unread,
Aug 6, 2008, 2:07:17 PM8/6/08
to vim...@googlegroups.com
* Kristoffer Stensbo-Smidt <digita...@gmail.com> 06.08.2008


> I would like Vim to change the color scheme according to the file type
> being edited. For example, I would like to use the color scheme
> mycolors1 for Python editing and mycolors2 for everything else. How do
> I achieve this?

Hello Kristoffer,

like this in your .vimrc

colorscheme=mycolors2
autocmd FileType python colorscheme=mycolors1

Hth Michael

--
Life is Xerox, and you're just a copy

Tim Chase

unread,
Aug 6, 2008, 2:28:41 PM8/6/08
to vim...@googlegroups.com
> like this in your .vimrc
>
> colorscheme=mycolors2
> autocmd FileType python colorscheme=mycolors1

I believe the current colorscheme is a vim-wide setting, so if
you've got a mix of python and non-python buffers open, you may
have to toggle the colorscheme on the BufEnter event. Something like

au BufEnter * exec 'colorscheme '.(&ft=='python' ? 'elflord' :
'peachpuff')

(okay...randomly chosen colorschemes, not asthetically chosen)
should allow you to toggle the colorscheme depending on the
buffer you're currently editing. Man, does that autocmd give me
a headache :)

-tim

Kristoffer Stensbo-Smidt

unread,
Aug 6, 2008, 2:28:50 PM8/6/08
to vim...@googlegroups.com
Hi Michael,

Thank you very much! It was almost correct. ;o)
Just for reference, the correct .vimrc lines should be

colorscheme mycolors2
autocmd FileType python colorscheme mycolors1

That is, without the equal signs ( = ).

Best regards,
Kristoffer

2008/8/6 Michael Wagner <michae...@web.de>:

Kristoffer Stensbo-Smidt

unread,
Aug 6, 2008, 4:45:01 PM8/6/08
to vim...@googlegroups.com
Wow, thank you Tim! That's exactly what I was looking for! Beautiful! :o)

/Kristoffer

2008/8/6 Tim Chase <v...@tim.thechases.com>:

Tony Mechelynck

unread,
Aug 6, 2008, 5:07:47 PM8/6/08
to vim...@googlegroups.com

Highlight groups, and therefore color schemes, apply to all files in all
windows of all tabs of one instance of Vim. If you have an HTML file in
one window, its CSS style sheet in another, and a page of Vim help in a
third, you cannot apply three different colorschemes to them. Similarly
for a Python script, a Vim script and a Vim helpfile being displayed
side-by-side.

However, each syntax script defines highlight groups groups with names
starting with the syntax name, then links as many as possible to
existing highlight groups.

To highlight python scripts otherwise than all others, you would thus
have to write your own colorscheme, because by default, colorschemes
accept the default links set by the syntax scripts: a colorscheme
defines Comment, then syntax/c.vim links cComment to it,
syntax/python.vim links pythonComment to it, syntax/vim.vim links
vimComment, and so on, all that with ":hi link default" statements,
which are applied only if the same group is not defined elsewhere. In
order to highlight python files differently than all others, you would
have to explicitly define pythonComment, pythonStatement, etc., maybe
define a new pythonLiteral group (or something) to which you would link
pythonString, pythonNumber, pythonRawString, etc. Check
$VIMRUNTIME/syntax/python.vim for the names which are atually used.


Best regards,
Tony.
--
Rudin's Law:
If there is a wrong way to do something, most people will do it
every time.

Kristoffer Stensbo-Smidt

unread,
Aug 7, 2008, 5:17:20 AM8/7/08
to vim...@googlegroups.com
2008/8/6 Tony Mechelynck <antoine.m...@gmail.com>:

> Highlight groups, and therefore color schemes, apply to all files in all
> windows of all tabs of one instance of Vim. If you have an HTML file in
> one window, its CSS style sheet in another, and a page of Vim help in a
> third, you cannot apply three different colorschemes to them. Similarly
> for a Python script, a Vim script and a Vim helpfile being displayed
> side-by-side.
>
> However, each syntax script defines highlight groups groups with names
> starting with the syntax name, then links as many as possible to
> existing highlight groups.
>
> To highlight python scripts otherwise than all others, you would thus
> have to write your own colorscheme, because by default, colorschemes
> accept the default links set by the syntax scripts: a colorscheme
> defines Comment, then syntax/c.vim links cComment to it,
> syntax/python.vim links pythonComment to it, syntax/vim.vim links
> vimComment, and so on, all that with ":hi link default" statements,
> which are applied only if the same group is not defined elsewhere. In
> order to highlight python files differently than all others, you would
> have to explicitly define pythonComment, pythonStatement, etc., maybe
> define a new pythonLiteral group (or something) to which you would link
> pythonString, pythonNumber, pythonRawString, etc. Check
> $VIMRUNTIME/syntax/python.vim for the names which are atually used.

I'm not sure I understand your point here. Tim's hack worked
perfectly: Opening, say, my .vimrc, an HTML document and a Python
script in three buffers applies my default colorscheme to the first
two files and a different colorscheme to the Python script. Vim also
remembers the different colorschemes when I change buffers.
If I understand your mail correctly, you say that Vim can't work this way?

Benjamin Fritz

unread,
Aug 7, 2008, 9:43:01 AM8/7/08
to vim...@googlegroups.com

If you use Vim with 1 window, editing 1 buffer at a time, then this
will certainly work fine.

If you use Vim with multiple split windows, possibly in multiple tabs,
this will not work.

Just try :help in one of your python buffers, and you will see that
the two windows use the same colorscheme.

Kristoffer Stensbo-Smidt

unread,
Aug 7, 2008, 10:08:15 AM8/7/08
to vim...@googlegroups.com
> If you use Vim with 1 window, editing 1 buffer at a time, then this
> will certainly work fine.
>
> If you use Vim with multiple split windows, possibly in multiple tabs,
> this will not work.
>
> Just try :help in one of your python buffers, and you will see that
> the two windows use the same colorscheme.

Ah, ok, didn't think about that. Well, I wanted this hack just to
avoid setting the colorscheme manually every time I open a python
script. It doesn't really matter if it is being inherited by other
split windows. :o)

Danny

unread,
Aug 10, 2008, 5:11:20 PM8/10/08
to vim_use
An aside. I was always curious if it was possible to have a color
scheme change in the same buffer depending on the current syntax being
applied. Example, I am writing an html file and when I create a css
section I would have liked to have the css section have a completely
different syntax scheme. So as I understand it, vim wouldn't support
this mechanism at all. At least not without runnin the syntax files
through a blender. Correct?

Danny.

On Aug 6, 5:07 pm, Tony Mechelynck <antoine.mechely...@gmail.com>
wrote:

Tim Chase

unread,
Aug 10, 2008, 5:28:08 PM8/10/08
to vim...@googlegroups.com
> An aside. I was always curious if it was possible to have a
> color scheme change in the same buffer depending on the
> current syntax being applied. Example, I am writing an html
> file and when I create a css section I would have liked to
> have the css section have a completely different syntax
> scheme. So as I understand it, vim wouldn't support this
> mechanism at all. At least not without runnin the syntax
> files through a blender. Correct?


You may be mixing the ideas of color schemes with
syntax-highlighting files. Syntax files usually identify blocks
of text as certain types (such as "identifier", "string",
"constant", "preprocessor command", etc). The colorschemes then
take these common types and map them to a color.

It's certainly possible to have CSS and JavaScript included in
the HTML, as shown in your $VIMRUNTIME/syntax/html.vim script.
It should properly map these to the types which your colorscheme
should then map to your colors of choice. As an example, the
following, saved as an HTML file:

--------------------
<html>
<head>
<title>Foo</title>
<script><!--
var i = 42;
alert("Hello world");
//--></script>
<style>
a {color:red;}
h1 {color:green;}
</style>
</head>
<body>
<h1>Works for me&reg;</h1>
</body>
</html>
--------------------

highlights the HTML with HTML syntax, the JavaScript as JS
syntax, and the CSS as CSS syntax -- all the way one would expect.

If not, you may be running an older version, or you might have
some setting that has disallowed Vim from behaving the way you
desire.

-tim

Tony Mechelynck

unread,
Aug 10, 2008, 5:32:37 PM8/10/08
to vim...@googlegroups.com
On 10/08/08 23:11, Danny wrote:
> An aside. I was always curious if it was possible to have a color
> scheme change in the same buffer depending on the current syntax being
> applied. Example, I am writing an html file and when I create a css
> section I would have liked to have the css section have a completely
> different syntax scheme. So as I understand it, vim wouldn't support
> this mechanism at all. At least not without runnin the syntax files
> through a blender. Correct?
>
> Danny.

You wouldn't need to touch the syntax files. What you would need is a
colorscheme (probably own-coded) which defines non-default highlights
for at least one set of the css* and html* highlights. Of course,
embedded CSS (as e.g. in a <style> tag in the <head> part of your HTML
page) would be highlighted as CSS.


Best regards,
Tony.
--
Dimensions will always be expressed in the least usable term.
Velocity, for example, will be expressed in furlongs per fortnight.

Charles E. Campbell, Jr.

unread,
Aug 10, 2008, 1:38:35 PM8/10/08
to vim...@googlegroups.com
Danny wrote:
> An aside. I was always curious if it was possible to have a color
> scheme change in the same buffer depending on the current syntax being
> applied. Example, I am writing an html file and when I create a css
> section I would have liked to have the css section have a completely
> different syntax scheme. So as I understand it, vim wouldn't support
> this mechanism at all. At least not without runnin the syntax files
> through a blender. Correct?
>
Short answer: no

However, your question seems to confuse colorschemes with
syntax-highlighting. You *can* have your css sections highlighted
differently. See how syntax/vim.vim handles including perl highlighting
inside vim script files:

if (g:vimsyn_embed =~ 'p' && has("perl")) &&
filereadable(expand("<sfile>:p:h")."/perl.vim")
unlet! b:current_syntax
syn include @vimPerlScript <sfile>:p:h/perl.vim
if exists("g:vimsyn_folding") && g:vimsyn_folding =~ 'p'
syn region vimPerlRegion fold matchgroup=vimScriptDelim
start=+pe\%[rl]\s*<<\s*\z(.*\)$+ end=+^\z1$+ contains=@vimPerlScript
syn region vimPerlRegion fold matchgroup=vimScriptDelim
start=+pe\%[rl]\s*<<\s*$+ end=+\.$+ contains=@vimPerlScript
else
syn region vimPerlRegion matchgroup=vimScriptDelim
start=+pe\%[rl]\s*<<\s*\z(.*\)$+ end=+^\z1$+ contains=@vimPerlScript
syn region vimPerlRegion matchgroup=vimScriptDelim
start=+pe\%[rl]\s*<<\s*$+ end=+\.$+ contains=@vimPerlScript
endif
else
syn region vimEmbedError start=+pe\%[rl]\s*<<\s*\z(.*\)$+ end=+^\z1$+
syn region vimEmbedError start=+pe\%[rl]\s*<<\s*$+ end=+\.$+
endif

Well, that's complicated some by making the inclusion optional (ie.
g:vimsyn_embed) and by optional folding (g:vimsyn_folding).
The important part is: (assuming no folding)

syn include @vimPerlScript <sfile>:p:h/perl.vim
syn region vimPerlRegion matchgroup=vimScriptDelim
start=+pe\%[rl]\s*<<\s*\z(.*\)$+ end=+^\z1$+ contains=@vimPerlScript

There's start and end triggers so vim can recognize where the different
syntax highlighting is to begin and end. The "syn include" sets up a
highlighting group (vimPerlScript) that will be highlighted with
syntax/perl.vim.

Regards,
Chip Campbell

Reply all
Reply to author
Forward
0 new messages