4

I'd like to map a button to toggle Vim's background between transparent (to see stuff on other apps) and dark.


My current settings

I'm using onedark theme. There's currently no other background setting.

(Part of) my .vimrc:

set encoding=utf-8
set nocompatible
set rnu nu
set wildmode=longest,list,full
set wildmenu
filetype plugin on
syntax on

" Theme
colorscheme onedark

" Toggle transparent background
let t:is_transparent = 0
function! Toggle_transparent()
    if t:is_transparent == 0
        hi Normal guibg=NONE ctermbg=NONE
        let t:is_transparent = 1
    else
        set background=dark
        let t:is_tranparent = 0
    endif
endfunction
nnoremap <F1> : call Toggle_transparent()<CR>

Stuff I've tried

Manually using :hi Normal guibg=NONE ctermbg=NONE didn't work.

I found this script (I've added it to my vimrc) which also doesn't make my background transparent and the toggling seems to fail after the first couple of toggles:

let t:is_transparent = 0
 25 function! Toggle_transparent()
 26     if t:is_transparent == 0
 27         hi Normal guibg=NONE ctermbg=NONE
 28         let t:is_transparent = 1
 29     else
 30         set background=dark
 31         let t:is_tranparent = 0
 32     endif
 33 endfunction
 34 nnoremap <F1> : call Toggle_transparent()<CR>

What's wrong with hi Normal guibg=NONE ctermbg=NONE? How can I actually make

Edit:

  • I'm using Manjaro KDE
  • I have gvim installed after vim
  • Transparency works fine in the terminal
6
  • 1
    You haven't told us the most important pieces of information... Why doesn't guibg=NONE ctermbg=NONE quite work? What do you see that's unexpected? Are you using gvim or terminal? Which OS/Desktop Environment? Which Terminal? Does transparency work in your terminal while you're using the shell? These would be useful in figuring out what could fix this for you...
    – filbranden
    Jan 13, 2020 at 13:32
  • What do you mean by "didn't quite work"?
    – Rich
    Jan 14, 2020 at 15:28
  • It changes the background color but doesn't quite make it transparent Jan 14, 2020 at 16:06
  • I still don't understand what you mean by "doesn't quite make it transparent". Can you see through the terminal window?
    – Rich
    Jan 16, 2020 at 17:41
  • I can't see through it. It's still full color. Jan 17, 2020 at 3:59

4 Answers 4

3

The toggling in the terminal vim works fine for me if I do:

let t:is_transparent = 0                     
function! Toggle_transparent_background()                      
  if t:is_transparent == 0                   
    hi Normal guibg=#111111 ctermbg=black                     
    let t:is_transparent = 1
  else
    hi Normal guibg=NONE ctermbg=NONE                    
    let t:is_transparent = 0                        
  endif                    
endfunction               
nnoremap <C-x><C-t> :call Toggle_transparent_background()<CR>
1

In order to back to colorscheme, I use this:

" Toggle background transparency
let t:isTransparent = 0
function! BGToggleTransparency()
  if t:isTransparent == 0
    hi Normal guibg=#111111 ctermbg=black
    set background=dark
    let t:isTransparent = 1
  else
    hi Normal guibg=NONE ctermbg=NONE
    let t:isTransparent = 0
  endif
endfunction
2
  • 1
    Welcome to Vi and Vim! What do you mean "in order to back to colorscheme"? Perhaps edit to clarify. And why use a tab-local variable?
    – D. Ben Knoble
    Jul 26, 2021 at 16:26
  • Thx! I use the github.com/crusoexia/vim-monokai theme and when I use a fixed color, like #111111 generally I miss the background colorscheme, and the background is a bit different from the #11111. I don't know exactly why, but it works pretty well when I toggle, e.g: " :: Toggle background transparency (F2) " ----------------------------------------------------------------------------- nnoremap <F2> : call BGToggleTransparency()<CR> Just because I call set background=dark again, I think Jul 27, 2021 at 23:56
1

The reason your script fails after a couple toggles is because the variable name is misspelled on line 31. It won't toggle transparency in gVim though, since there is no built in transparency there. You'll need help for that. This should toggle transparency for both terminal vim and gVim:

" This makes vim transparent in terminal by default
hi Normal ctermbg=NONE

let t:is_transparent = 0
function! Toggle_transparent()
   if t:is_transparent == 0
       if has ('gui_running')
           " Sets gVim to transparent
           :silent !transset -a --dec 0.15 
       else
           " Sets vim to opaque
           set background=dark
       endif
       let t:is_transparent = 1
   else
       if has ('gui_running')
           " Sets gVim to opaque
           :silent !transset -a --max 1 
       else
           " Sets vim to transparent
           hi Normal ctermbg=NONE
       endif
       let t:is_transparent = 0
   endif
endfunction
nnoremap <C-t> : call Toggle_transparent()<CR>

I like my terminal to default to transparent and gVim to default to opaque. If you prefer different behavior swapping some things around should get you there. Edit the number after --dec to set the transparency level where you want it. This works on Debian, with transset from the x11-apps package. On Manjaro, don't know.

0

I just want to provide the NeoVim version answer, in case anyone (include myself) would need it in the future:

local _get_darken = false
function _G.toggle_background_transparent()
  if (not _get_darken) then
    vim.cmd("hi Normal guibg=#151515")
    _get_darken = true
  else
    vim.cmd("hi Normal guibg=NONE")
    _get_darken = false
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.