I have set up Vim to trigger reformatting on save using ALE, but sometimes that doesn't fly very well, and would like to disable it manually. But remembering what the right incantations are is always impossible, needing to grep my dotfiles for clues, so I'd like to be able to toggle it, say, using Alt-F8. That means some kind of vimscript function that just sets let g:ale_fix_on_save = 0.

How would such a function look and how do I bind it?

(Bonus points for showing how to toggle the value)

  • It sounds like you're asking for a rudimentary mapping with a single assignment statement. Are you having trouble setting this up? What have you tried? FYI, you can toggle with let x = !x.
    – B Layer
    Dec 3 '19 at 15:43

[While B Layer is right that this question should demonstrate attempts (in order that answers address conceptual misunderstandings), the answer is at least straightforward.]

To toggle a "boolean" variable:

let x = !x

So the mapping is simply

nnoremap <your keys> :let g:ale_fix_on_save = !g:ale_fix_on_save<CR>
  • 2
    Thank you. Of course I had to try and make this work using the Alt key plus F keys, which Google quickly showed involved a world of pain trying to make work in ever changing environments. Mapped it to F9 to avoid further troubles.
    – oligofren
    Dec 3 '19 at 22:50
  • I'm having trouble with the first sentence. BLayer is right...but...what?
    – B Layer
    Dec 3 '19 at 23:57
  • @BLayer do you mean you have trouble with its meaning or parsing the structure? If former, perhaps I meant to really say that I was asking the same questions as you were in the comments, but Im going to answer anyway—if latter, a stripped version is « while BLayer is right (blah), the answer is at least straightforward »
    – D. Ben Knoble
    Dec 4 '19 at 0:00
  • @BLayer I’ve edited slightly
    – D. Ben Knoble
    Dec 4 '19 at 0:01
  • 1
    Yeah, you're right. I'm definitely not a fan of the emergence of point-grabbing as the primary motivator that seems to be the new norm (esp. among some newer arrivals).
    – B Layer
    Dec 4 '19 at 0:29

Your Answer

