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


#1

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?


#2

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…?


#3

Add this to your init.coffee file:

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

Then create the shortcut in your keymap.coffee file:

'.editor:not(.mini)':
  'f1': 'fold:toggle'

#4

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).


#5

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


#6