Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

vim - Automatically change colorscheme based on FileType

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...