I have two functions, one using an if statement and one using (or trying to use) a ternary expression:

nnoremap <expr> Z Toggle1()
func Toggle1 ()
    if g:colors_name == "OceanicNext"
        return ":colorscheme TextMate^M"
        return ":colorscheme OceanicNext^M"

nnoremap <expr> Q Toggle2()
func Toggle2 ()
    echom g:colors_name
    let cs = "TextMate" ? (g:colors_name == "OceanicNext") : "OceanicNext"
    let cmd_string = printf(":colorscheme %s^M", cs)
    return cmd_string

The first one works, but the second one only 'works' the first time I run it. What's the difference between the two, and what am I doing wrong in the second one?

  • The ternary operator is borrowed from C. It's syntax condition ? value1 : value2 – Matt Jun 13 '20 at 6:56
  • @Matt -- sure, but that's what I have here, right? – David542 Jun 13 '20 at 6:58
  • Just look at your code: what stands before "question" and what after it? – Matt Jun 13 '20 at 6:59
  • @Matt d'oh! I see, thanks for pointing that out. – David542 Jun 13 '20 at 7:01
  • @Matt based on your other answers, I ended up using this for the mapping: nnoremap <expr> CC g:colors_name == "oceanicnext" ? ":colorscheme textmate^M" : ":colorscheme oceanicnext^M" – David542 Jun 13 '20 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.