简体   繁体   中英

set a colorscheme for a filetype in vim

Within vim, I am trying to use a special colorscheme for some filetypes using an autocmd. Strangely, that does not work for all the filetypes. Here is my vimrc:

autocmd filetype troff :colorscheme troff
autocmd filetype tintin :colorscheme troff
autocmd BufNewFile,BufRead *.tt set ft=tintin
autocmd BufNewFile,BufRead *.tr set ft=troff

While openning f.tr, the colorscheme "troff" is used, but while openning f.tt, while the filetype is correctly set to "tintin", the default colorscheme is used. If I manually set the filetype (sef ft=tintin), then the colorscheme troff is loaded. Could you please help me to figure what could cause that strange behaviour?

I cannot reproduce your problem. However I would suggest the following auto commands:

autocmd BufNewFile,BufRead *.tt let g:tmpcolor=g:colors_name            
autocmd BufNewFile,BufRead *.tr let g:tmpcolor=g:colors_name            
autocmd BufEnter *.tt colorscheme troff | set ft=tintin                  
autocmd BufEnter *.tr colorscheme troff | set ft=troff                   
autocmd BufLeave *.tt exe 'colorscheme '.g:tmpcolor                     
autocmd BufLeave *.tr exe 'colorscheme '.g:tmpcolor

This will create a variable g:tmpcolor that stores the original color scheme. When you edit a file of type .tt or .tr the color scheme will change to troff . When you leave these files, the color scheme will change to g:tmpcolor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM