2
votes

I have some prefered colorscheme on some filetypes, but when I open another file (different file type with different colorscheme) and come back on a previous one, the new colorscheme persists.

I have this in my Gvimrc (using gvim)

autocmd WinEnter,FileType * colorscheme default
autocmd WinEnter,FileType c,cpp colorscheme darkblue
autocmd WinEnter,FileType R,r colorscheme desert

How can I modify to make sure that colorscheme persists according to filetype when I navigate across buffers ?

EDIT: adding the following did not fix

autocmd BufEnter,BufNew,FileType c,cpp colorscheme darkblue
autocmd BufEnter,BufNew,FileType R,r colorscheme desert
2
It seems you should add BufNew,BufEnter autocmds :>MPogoda

2 Answers

7
votes

It does not make sense to combine WinEnter and FileType events in the same autocmd rule; the first matches against the buffer's filename while the latter matches against the buffer's filetype.

Instead, use a single autocmd triggered whenever a buffer is entered / displayed in a window, and choose the colorschmeme with a conditional on the &filetype.

:autocmd BufEnter,FileType *
\   if &ft ==# 'c' || &ft ==# 'cpp' | colorscheme darkblue |
\   elseif &ft ==? 'r' | colorscheme desert |
\   else | colorscheme default |
\   endif
7
votes

Ingo's answer is smarter but these autocommands

autocmd BufEnter *     colorscheme default
autocmd BufEnter *.php colorscheme desert
autocmd BufEnter *.py  colorscheme darkblue

should work. Well, they work, here.