How can I use the same shortcut for Fold and Unfold?


I would like to use F1 as the shortcut for both folding and unfolding a line. I tried setting up the same shortcut for both commands, but after reading the docs I understand why that doesn’t work.

Is there any way to achieve this kind of key binding?


At a guess, something along the lines of creating a custom command “fold-toggle” that will check if the code at the cursor position is folded and then unfold if so, otherwise fold and then setting F1 to that command, kinda thing…?


Add this to your file:

atom.workspaceView.command 'fold:toggle', ->
  editor = atom.workspace.getActiveEditor()
  row = editor.getCursor().getBufferRow()
  if editor.isFoldedAtBufferRow(row)

Then create the shortcut in your file:

  'f1': 'fold:toggle'


Here’s the new one for 1.0:

It uses new command syntax and also removes the need for the getActiveEditor() which is now getActiveTextEditor() btw. It also uses @getModel() (cleaner).


Closing this as duplicate in favor of the more recent one.