set vim background transparent
Solution 1:
You don't have to change anything in your colorscheme just add the following to your .vimrc:
hi Normal guibg=NONE ctermbg=NONE
Update:
As Liam mentioned in the comments:
This line needs to go below the
colorscheme
in.vimrc
Solution 2:
If you load a plugin at line 5 of your .vimrc for example, then if you change line 6, it doesn't mean that Vim load the plugin completely and then run your line 6!!
That's why, you should use autocmd
command, because in this case, it ensures that all of your plugins are loaded completely and then your command will run after that!
In this case:
" transparent bg
autocmd vimenter * hi Normal guibg=NONE ctermbg=NONE
" For Vim<8, replace EndOfBuffer by NonText
autocmd vimenter * hi EndOfBuffer guibg=NONE ctermbg=NONE
Now you sure that after all the things are loaded, you are running your commands.
Solution 3:
Use this gist. I compile some settings to make vim transparent.
Solution 4:
The answers above don't solve all the problems, they change the bg to transparent when we enter vim (hence the "VimEnte" event) but when you source your init.vim
file again, the background reverts back (this is because when the file is sourced the VimEnter auto command is not executed).
Instead of directly posting the correct answer, I'll explain how to reach it:
So, First, we need to understand what happens when vim is open:
vi -V10debug.log +q
This will create a debug.log where you can see what auto commands are executed and their order.
autocmd vimenter * hi Normal guibg=NONE ctermbg=NONE " For Vim<8, replace EndOfBuffer by NonText autocmd vimenter * hi EndOfBuffer guibg=NONE ctermbg=NONE```
If we are using this, we see in the log that VimEnter change the bg to NONE (so far its good).
But, and the following command opens vim, then source the vimrc and then exit (for faster finding I have putted some print statements)
vi -V10debug_so.log +'!echo sourcing' +'source ~/.config/nvim/init.vim' +'!echo sourced' +q
In the new log, we see, after so VimEnter is not called again and the bg is reverted back to theme default.
But, We also can notice when a file is sourced there are some events that occur, we will focus on the following
- SourcePre - before sourcing
- SourcePost - after sourcing
There the above incomplete solutions can be fixed using the SourcePost
event. so the new and correct autocommand is (Final Answer)
" Workaround for creating transparent bg
autocmd SourcePost * highlight Normal ctermbg=NONE guibg=NONE
\ | highlight LineNr ctermbg=NONE guibg=NONE
\ | highlight SignColumn ctermbg=NONE guibg=NONE
Always use this in a group, see this for as reference - https://github.com/kalkayan/dotfiles/blob/main/.config/nvim/init.vim