Vim: Autocmd for removing colorscheme background fail to run

Tag: vim , terminal , autocmd Author: Bit0602121020 Date: 2012-08-31

I'm trying to override the background of all colorschemes when using terminal windows - mostly because I find transparent terminal windows strangely exciting. All my best efforts of research points towards defining an autocmmand that will run everytime the colorscheme is changed. Currently the command looks like this:

autocmd ColorScheme * so rmbackground.vim

rmbackground.vim is a file of my own making which basically lists a bunch of hilightcommands like so:

hi Normal ctermbg=NONE
hi Comment ctermbg=NONE

Now the strange thing is that everything works correctly if I source the rmbackground.vim file manually, but the autocommand won't run as expected. Nor does it work if I specify a single hilight command as an autocmd instead of sourcing the whole rmbackground-file.

I feel very confused about the whole issue. below is my full vimrc and rmbackground.vim in case there are conflicts I'm not aware of. (The file is hideous at the moment since I've just started building my config but I hope you'll forgive me ;-) )


runtime bundle/vim-pathogen/autoload/pathogen.vim
call pathogen#infect()

"Turn on syntax
if has("syntax")
  syntax on

"Jump to last edited line
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

"Indent correctly
if has("autocmd")
  filetype plugin indent on

set t_Co=256 "use 256 colors
let g:CSApprox_attr_map = { 'bold' : 'bold', 'italic' : '', 'sp' : '' }

set background=light
colorscheme twilight

set showcmd     " Show (partial) command in status line.
set showmatch       " Show matching brackets.
set ignorecase      " Do case insensitive matching
set smartcase       " Do smart case matching
set hlsearch        " Highlight search-terms
set incsearch       " Incremental search
set autowrite       " Automatically save before commands like :next and :make
set hidden             " Hide buffers when they are abandoned
set mouse=a     " Enable mouse usage (all modes)

set history=1000

runtime macros/matchit.vim

set wildmenu
set wildmode=list:longest

set title
set scrolloff=3
set backupdir=~/.vim/tmp,~/.tmp,/var/tmp,/tmp
set directory=~/.vim/tmp,~/.tmp,/var/tmp,/tmp
set ruler

set backspace=indent,eol,start

set tabstop=4
set softtabstop=4
set shiftwidth=4
set shiftround "Round the indention nicely with other indents

set visualbell

autocmd ColorScheme * so rmbackground.vim


"Remove background set by colorscheme
hi Normal ctermbg=NONE
hi Comment ctermbg=NONE
hi Constant ctermbg=NONE
hi Special ctermbg=NONE
hi Identifier ctermbg=NONE
hi Statement ctermbg=NONE
hi PreProc ctermbg=NONE
hi Type ctermbg=NONE
hi Underlined ctermbg=NONE
hi Todo ctermbg=NONE
hi String ctermbg=NONE
hi Function ctermbg=NONE
hi Conditional ctermbg=NONE
hi Repeat ctermbg=NONE
hi Operator ctermbg=NONE
hi Structure ctermbg=NONE

Best Answer

The commands in .vimrc are executed sequentially. You're first setting :colorscheme, but the :autocmd comes only at the end.

Either you move the autocmd to before the :colorscheme command (so that it already applies to that), or you explicitly :source rmbackground.vim after the :colorscheme command, and have the :autocmd only for later dynamic changes.


So I put the autocmd line right after the pathogen stuff and changed source to runtime like you adviced me unfortunately, it doesn't seem to make any difference. I even put runtime rmbackground.vim at the very end (which I take to mean that rmbackground.vim would be sourced twice on startup) but no luck with that either... :P
Perhaps it might not be something in my vimrc that's wrong (after the suggested modifications) but due to the CSApprox plugin I'm using?
Yes, CSApprox modifications are triggered after .vimrc, that's a probable cause. You should have told us you're using that plugin :-) Disable it or try putting the :runtime into ~/.vim/after/plugin/zzz.vim
I made the file .vim/after/plugin/CSApprox.vim and put the contents of rmbackground.vim in that file and that works for whenever I start a new instance of vim. Good enough for me! Thanks!
Thanks! I got bit by CSApprox too.