1

I'm trying to assign a key toggle to switch between dark or light background. I wrote this function:

function ToggleBG()
    let l:bg='set background?'
    if l:bg==dark
        :set background=light
    elseif l:bg==light
        :set background=dark
    endif    
endfunction

nmap <F6> :call ToggleBG()<cr>

It doesn't work, error,

E121: Undefined variable: dark  
E15: Invalid expression: l:bg==dark

The logic is, retrieve current bg setting set background?, put it in a local variable, l:bg; if it's dark switch it to light and vice versa.
Assign the function to F6 key.

Using Vim 8.1 and Neovim. Any ideas how will this work?

2

1 Answer 1

2

Try to execute several times in a command line:

:let &bg=(&bg=='light'?'dark':'light')

It will change background from light to dark to light...

Then map it to F6:

nnoremap <F6> :let &bg=(&bg=='light'?'dark':'light')<cr>

Check

  • :help :let-& -- on how to access option values in vimscript.
  • :help trinary -- for expr ? expr : expr description
1
  • It was that simple! Thank you!
    – Krackout
    Commented Oct 29, 2020 at 17:57

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.