根据文件类型自动更改配色方案 [英] Automatically change colorscheme based on FileType
问题描述
我已阅读此SO 问题>
我还执行了以下帮助主题::h BufEnter
:h BufNewFile
:h BufLeave
...但仍然 - 我在 vimrc
中有以下内容 - 它是如何工作的?
colo pyte自动命令!BufEnter,BufNewFile *.sql colo Zenesque自动命令!BufLeave *.sql colo pyte
如果我打开一个 .sql
文件,我假设它正在执行 BufNewFile
并因此应用配色方案 Zenesque?如果我然后离开该缓冲区并将光标放在 vimrc 的缓冲区中,那么 BufLeave
行肯定会执行,但是它将 pyte 应用到哪个缓冲区,以及为什么我指定 .sql
BufLeave
旁边的代码>?
Colorschemes are global;它们的颜色同时适用于所有窗口.因此,您只能为每个缓冲区/文件类型/窗口创建不同颜色方案的错觉,当您同时看到不同的颜色方案时,这种错觉就会被打破.如果您不使用窗口拆分或只使用相同颜色的拆分同时可见,那么您的 :autocmd
解决方案应该可以工作.
BufLeave
是 BufEnter
的对应物;BufNewFile
只是新缓冲区的一个特例.所有都具有相同的 *.sql
模式,因此它们可以相互撤消.例如,当您从 .vimrc
移动时,BufEnter
将执行.当您返回时,BufLeave
将执行.对于您的 .vimrc
缓冲区,类似的事件将触发,但它们背后没有任何操作,因为您尚未为 *.vim
定义类似的自动命令.
注意:如果您想将切换扩展到更多文件类型和颜色方案,这个答案可能会有所帮助.>
I've read this SO question
I've also executed the following help topics:
:h BufEnter
:h BufNewFile
:h BufLeave
...but still - I have the following in vimrc
- how does it work?
colo pyte
autocmd! BufEnter,BufNewFile *.sql colo Zenesque
autocmd! BufLeave *.sql colo pyte
If I open a .sql
file I assume that its executing BufNewFile
and therefore applies the color scheme Zenesque? If I then leave that buffer and place the cursor in the buffer for vimrc then surely the BufLeave
line will execute but which buffer does it apply pyte to, and why do I specify .sql
next to BufLeave
?
Colorschemes are global; their colors apply to all windows at the same time. Therefore, you can only create the illusion of different colorschemes per buffer / filetype / window, which will be shattered when you have different ones visible at the same time. If you don't use window splits or only ever have splits using the same colorscheme visible at the same time, your solution with :autocmd
should work, though.
The BufLeave
is the counterpart of BufEnter
; the BufNewFile
is just a special case for new buffers. All have the same *.sql
pattern so that they undo each other. For example, when you move from your .vimrc
, the BufEnter
will execute. When you move back, the BufLeave
will execute. For your .vimrc
buffer, similar events will fire, but there's no action behind them, since you haven't defined similar autocmds for *.vim
.
Note: If you want to extend your switching to more filetypes and colorschemes, this answer may be helpful.
这篇关于根据文件类型自动更改配色方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!