Disabling Multiple Keybindings with a Single Key?


#1

Hello, I’ve been impressed with a writing feature in several apps called “Hemmingway” which essentially disabled your ability to delete or move around text. I thought the simplest solution to implement this myself would be to have a keybinding which maps the deletion and movements keys to nothing. However, I have not been able to figure out whether the API is able to disable and enabled keybindings from the init.coffee-script, or whether a keymap.cson-binding may be mapped to more than one action.

If you have any solution for this please let me know, thank you.


#2

There is information on this (and many other things) in the Atom Flight Manual:

http://flight-manual.atom.io/behind-atom/sections/keymaps-in-depth/#removing-bindings


#3

It’s not easy to target a specific keybinding and turn it on or off via the API. What you can do is disable an existing keybinding and make commands that refers to the commands that you want to turn on and off.

atom.commands.add 'atom-workspace', 'custom:dummy-backspace', ->
  return if atom.config.get('user.commands-toggled-off') # If the commands are toggled off, then don't proceed

  atom.commands.dispatch 'atom-workspace', 'core:backspace'

It’s that simple. It’s better to have the toggle be on by default so that you don’t temporarily lose a bunch of commands if you happen to delete your config.cson (it wouldn’t be a problem at all, just a violation of expectations). And then to actually toggle the commands:

atom.commands.add 'atom-workspace', 'custom:toggle-movement-functions', ->
  if atom.config.get('user.commands-toggled-off')
    atom.config.unset('user.commands-toggled-off')
  else
    atom.config.set('user.commands-toggled-off', '1')

In my keymap.cson:

'atom-text-editor':
  'backspace': 'custom:dummy-backspace'
  'ctrl-t': 'custom:toggle-movement-functions'

Do that several times and you have a toggleable set of commands.


#4

The toggle reminds me of how commands look in “Vim Mode.” I’ll try it out and report back when I’m done, thanks.