0

I have the following script but not working:

  1. I don't know what's the standard way to inverse the boolean(I used number 1/0).
  2. I not sure whether it's correct to put global variable, which I used to store plugin state, in .vimrc is a bad idea.
  3. Why the error?

function! s:update_state() abort
    let old_mini = g:minimap_is_opened
    let old_fern = g:fern_is_opened
    g:minimap_is_opened == 0? g:minimap_is_opened = 1 : g:minimap_is_opened = 0
    g:fern_is_opened == 0? g:fern_is_opened = 1 : g:fern_is_opened = 0
    if old_mini==1 && old_fern==0
        return ":MinimapToggle\<CR>:Fern . -drawer -width=36 -toggle -reveal=%\<CR>"
    elseif old_fern==1 && old_mini==0
        return "\<CR>:Fern . -drawer -width=36 -toggle -reveal=%\<CR>:MinimapToggle"
    endif
endfunction

Usage:

nnoremap <silent><nowait><expr> <Tab> <SID>update_state()

But I got error:

Error detected while processing function <SNR>2_update_state:
line    3:
E492: Not an editor command: minimap_is_opened = 1 : g:minimap_is_opened = 0
1
  • 1
    You are using the ternary ? : operator in the wrong context. You need to do it this way: let g:minimap_is_opened = g:minimap_is_opened == 0 ? 1 : 0 -- Of course, it's much easier just to do: let g:minimap_is_opened = !g:minimap_is_opened – Heptite Apr 2 at 21:41
3

You can invert a boolean with !:

let foo = 1
echo foo " will print 1
let foo = !foo
echo foo " will print 0

I'm afraid I can't help much with the overall function as I am unfamiliar with minimap, but that might help you get started. Note that the syntax in your ternary operatory is incorrect. If you are doing it that way you want:

let foo = foo == 0 ? 1 : 0
3
  • Can your last line simplified to let foo = !foo ? 1 : 0? I mean: is that every non-zero number treated as a true? – job_start Apr 2 at 19:39
  • 1
    @job_start Yes, or let foo = foo ? 0 : 1. You might find it helpful to read Learn Vimscript the Hard Way if you haven't already, particularly Chapter 21 which deals with "truthiness" and conditional statements. – Andrew Ho-Lee Apr 2 at 20:40
  • worth pointing out here that in "classic" vim script uses numbers 1 and 0 for true and false. So the result of ! is always 1 or 0. – Mass Apr 2 at 22:14

Your Answer

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

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