且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据文件类型自动更改配色方案

更新时间:2023-02-08 19:23:31

Colorschemes are global;它们的颜色同时适用于所有窗口.因此,您只能为每个缓冲区/文件类型/窗口创建不同颜色方案的错觉,当您同时看到不同的颜色方案时,这种错觉就会被打破.如果您不使用窗口拆分或只使用相同颜色的拆分同时可见,那么您的 :autocmd 解决方案应该可以工作.

BufLeaveBufEnter 的对应物;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.