开发者

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 B开发者_StackOverflow中文版ufEnter *
    \ 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜