文件类型更改时颜色方案更改 [英] Colorscheme change when filetype changes

查看:122
本文介绍了文件类型更改时颜色方案更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开.vim,.htm,.html,.xml或_vimrc文件时,是否可以在Vim中更改颜色?
(当我切换到具有上述扩展名的已打开文件时,也会更改colorscheme)

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)

这就是我希望我的vim要做的事情:

This is what I want my vim to do:

file = txt或新的(未保存)缓冲区:colorscheme1(默认colorscheme)

file = vimrc,vim,html,htm:colorscheme2

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

我还注意到,我的vim不能检测所有文件类型。

文本文件未被识别为文本文件。

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

ps:

如果不可能,是否可以在我重新启动VIM时切换到其他颜色方案并保留它(不更改vimrc中的内容)?

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)?

编辑

使用autocmd找到解决方案:

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?

推荐答案

您应该能够通过以下自动命令实现接近您想要的功能:

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>

当然,您可以根据需要修改文件扩展名列表。颜色方案为< colourscheme1> 的默认设置,或< colorscheme2> 的默认设置,如果您打开(或切换到)任何指定的类型,将在Vim会话中打开(或切换到)其他任何类型时,请恢复为< 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.

希望这会有所帮助。

这篇关于文件类型更改时颜色方案更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆