set vim background transparent
Asked Answered
T

6

82

I am using the Matrix colorscheme along with CSApprox for my terminal vim.

I can not seem to be able to set the background as transparent. I have tried editing the matrix.vim file but it doesn't make it any better.

here is the matrix.vim

" vim:set ts=8 sts=2 sw=2 tw=0:
"
" matrix.vim - MATRIX like colorscheme.
"
" Maintainer: MURAOKA Taro <[email protected]>
" Last Change:  10-Jun-2003.

set background=dark
hi clear
if exists("syntax_on")
syntax reset
endif

let g:colors_name = 'matrix'

hi Comment guifg=#226622
hi Constant guifg=#55ff55
hi Special guifg=#44cc44
hi Identifier guifg=#55ff55
hi Statement guifg=#55ff55 gui=bold
hi PreProc guifg=#339933
hi Type guifg=#55ff55 gui=bold
hi Underlined guifg=#55ff55 gui=underline
hi Error guifg=#55ff55
hi Todo guifg=#113311 gui=none
hi Cursor guifg=#226622
hi lCursor guifg=#226622
hi CursorIM guifg=#226622
hi Directory guifg=#55ff55
hi DiffAdd guifg=#55ff55 gui=none
hi DiffChange guifg=#55ff55 gui=none
hi DiffDelete guifg=#113311 gui=none
hi DiffText guifg=#55ff55 gui=bold
hi ErrorMsg guifg=#55ff55
hi VertSplit guifg=#339933
hi Folded guifg=#44cc44
hi FoldColumn guifg=#44cc44
hi IncSearch guifg=#226622 gui=none
hi LineNr guifg=#44cc44 gui=none
hi ModeMsg guifg=#44cc44
hi MoreMsg guifg=#44cc44
hi NonText guifg=#44cc44 guibg=NONE ctermbg=none
hi Normal guifg=#44cc44 guibg=NONE ctermbg=none
hi Question guifg=#44cc44
hi Search guifg=#113311 gui=none
hi SpecialKey guifg=#44cc44
hi StatusLine guifg=#55ff55 gui=none
hi StatusLineNC guifg=#113311 gui=none
hi Title guifg=#55ff55 gui=bold
hi Visual guifg=#55ff55 gui=none
hi VisualNOS guifg=#44cc44
hi WarningMsg guifg=#55ff55
hi WildMenu guifg=#226622

and my .vimrc file

set nocompatible
filetype off

set rtp+=~/.vim/bundle/Vundle.vim/
call vundle#begin()

Plugin 'gmarik/Vundle.vim'
Plugin 'fatih/vim-go'
Plugin 'vim-airline/vim-airline'
" Plugin 'vim-airline/vim-airline-themes'
Plugin 'airblade/vim-gitgutter'
" Plugin 'altercation/vim-colors-solarized'
Bundle 'morhetz/gruvbox'
Plugin 'tpope/vim-git'
Plugin 'Valloric/YouCompleteMe'
Plugin 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
" Plugin 'flazz/vim-colorschemes'
Plugin 'godlygeek/csapprox'

call vundle#end()

filetype plugin indent on
syntax on
syntax enable

#...
#...
#...
#...
#...

set t_Co=256
colorscheme matrix

if i enter hi Normal guifg=#44cc44 guibg=NONE ctermbg=none in the command prompt, it looks as expected. but not when it's only declared in matrix.vim. i also tried adding it after colorscheme matrix in .vimrc, but it does not help.

How it looks like when first loaded.

enter image description here

How it looks like after i enter command

enter image description here

Telegraphy answered 8/6, 2016 at 20:54 Comment(0)
I
189

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

Isopropanol answered 9/6, 2016 at 8:32 Comment(10)
That's technically correct but i believe that codingninja is searching for a way t do it directly in a colorscheme. Or an explanation of why it cannot be done in a colorshemeDeepseated
When I enter this command in the prompt it works perfectly, but when I just add it to my .vimrc, it does not work.Telegraphy
There are chances like, if there is a highlight command in your .vimrc file that will overwritten the highlight command in your color scheme file. This will happen if you are set the colorscheme in your .vimrc before the overwritten highlight command in your .vimrc. If you wants to highlight the vim same as that of your colorscheme, write the set colorscheme command at the end of the .vimrc file.Estuarine
You need to use an autocmd on the ColorScheme event overriding the colorscheme options.Vitriform
I add 2 lines and it worked. ``` hi NonText ctermbg=none hi Normal guibg=NONE ctermbg=NONE ```Cattery
This line needs to go below the colorscheme in .vimrcKrystakrystal
You can add that line at the end of the color.vim file.Reachmedown
@TanDat I just did what you told and it worked! But I cannot set the background for numbers to NONE. I tried the Number group but it didn't work. I just want the line number background to be NONE. TIA.Sclerenchyma
This works very well using it in Awesome Vim Colorschemes on NeoVimRockbound
It needs to go below syntax on, not just below colorscheme.Fleischer
E
27

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.

Euglena answered 12/8, 2020 at 18:5 Comment(4)
it works great but I still have a non transparent bg after the end of the file. it is visible when the file has less lines than the screen height. any advise?Hermes
@Hermes if that background is related to your terminal, simply config your terminal to support transparency, if you are using graphical gvim, you can use something like picom, or compton to transparent them.Euglena
@Hermes try the following (if vim < 8, replace EndOfBuffer with NonText) autocmd vimenter * hi Normal guibg=NONE ctermbg=NONE autocmd vimenter * hi EndOfBuffer guibg=NONE ctermbg=NONEWadmal
This doesn't work if we resource the init.vim file (which we do very often): use SourcePost instead of VimEnter. see this https://mcmap.net/q/241836/-set-vim-background-transparent answer for more info.Skull
S
3

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

  1. SourcePre - before sourcing
  2. 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

Skull answered 17/5, 2021 at 12:7 Comment(0)
F
3

Pure LUA version for NeoVim to set fully-transparent background:

vim.api.nvim_set_hl(0, "Normal", {guibg=NONE, ctermbg=NONE})
Fetish answered 16/12, 2022 at 20:10 Comment(0)
B
1

In my case, I've just added this 2 line just after the colorscheme declaration :

hi NonText ctermbg=none
hi Normal guibg=NONE ctermbg=NONE
Bernitabernj answered 12/11, 2022 at 12:24 Comment(0)
C
0

I use macvim, and the hi Normal guibg=NONE ctermbg=NONE don't work for me even I put it after the colorscheme in .vimrc

But I found :set transparency=20 | :set blurradius=90 work quite good for me. From the help of macvim, they do state that this two commands are {not in Vi} and {only in MacVim GUI}.

Corinnacorinne answered 22/6, 2021 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.