Reman
Reman

Reputation: 8109

Colorscheme change when filetype changes

Is it possible to change the colorscheme in Vim when I open a .vim, .htm, .html, .xml or the _vimrc file? (also change colorscheme when I switch to an already open file with above extension)

This is what I want my vim to do:

file = txt or a new (not saved) buffer: colorscheme1 (default colorscheme)
file = vimrc, vim, html, htm: colorscheme2

I noted also that my vim doesn't detect all filetypes.
Text files are not recognized as text file.

ps:
If this is not possible would it be possible to switch to another colorscheme and keep it when I restart VIM (without changing something in vimrc)?


edit:

Found the solution with an autocmd:

augroup filetype_colorscheme
    au BufEnter *
    \ if !exists('b:colors_name')
        \ | if &ft == "vim" || &ft == "html"
            \ | let b:colors_name = 'colorscheme 2'
        \ | else
            \ | let b:colors_name = 'colorscheme 1'
        \ | endif
    \ | endif
    \ | exe 'colorscheme' b:colors_name
augroup END 

still one problem:
above code changes the colorscheme of all files in a split window when I click in a buffer

Is there a way to desactivate (and reactivate) an autocmd using a shortcut key?

Upvotes: 3

Views: 2278

Answers (1)

Prince Goulash
Prince Goulash

Reputation: 15715

You should be able to achieve something close to what you want with the following autocommands:

colo <colourscheme1>  "default
autocmd! BufEnter,BufNewFile *.html,*.vim,*xml colo <colourscheme2>
autocmd! BufLeave *.html,*.vim,*xml colo <colourscheme1>

And of course you can modify the list of file extensions as you wish. The colourscheme will be <colourscheme1> by default, or <colourscheme2> if you open (or switch to) any of the specified types, and will revert to <colourscheme1> when you open (or switch to) any other type within a Vim session.

Hope this helps.

Upvotes: 9

Related Questions