Search code examples
vim

How to set a different colorscheme for each file type in Vim?


In Vim, I want to use a different colorscheme for each file type.

e.g. I want to use desert256 colorscheme for Python & JavaScript files, and use jellybeans colorscheme for HTML & CSS files.


I've tried putting the following code in my .vimrc, but the colorscheme change happens only when changing buffers for the first time.

i.e. If I open a new Python file, Python's colorscheme is used, and when I open a new CSS *buffer*, indeed the colorscheme changes to CSS's colorscheme. However, Changing back to Python's buffer does not change the colorscheme back.

I've used autocmd WinEnter to try and make this rule happen when changing windows (and buffers), but it doesn't help:

autocmd WinEnter,FileType python,javascript colorscheme desert256
autocmd WinEnter,FileType *,html,css        colorscheme jellybeans  " This includes default filetype colorscheme.

How can I fix this? In addition, a bonus would be to not change a colorscheme when not needed - i.e. Changing from a Python to a JavaScript buffer won't change the colorscheme to "itself".


EDIT:

If anyone's interested, here is my .vimrc repo in github.com. I'll update it with the solution I find here once given.


Solution

  • I've been looking for the same thing. This inside your .vimrc works reasonably well although not perfect.

    autocmd BufEnter * colorscheme default
    autocmd BufEnter *.php colorscheme Tomorrow-Night
    autocmd BufEnter *.py colorscheme Tomorrow
    

    (Note if you're looking for a good dark color theme Tomorrow-Night looks pretty good. Very similar to theme used on Code Academy.)