How to toggle current fold in editor view?


I’d like to have editor:fold-current-row and editor:unfold-current-row combined. Is there any function that does exactly this?



Add this to your user init script:

atom.workspaceView.command 'editor:toggle-current-row-folding', ->
  e = atom.workspace.getActiveEditor()
  bufferRow = e.bufferPositionForScreenPosition(e.getCursorScreenPosition()).row
  if e.isFoldedAtBufferRow(0)

It’ll add a new command that toggle the fold at the current cursor position :).


Thank you, Abe. It works like a charme. Just a tiny correction was necessary:

--- if e.isFoldedAtBufferRow(0)
+++ if e.isFoldedAtBufferRow(bufferRow)


Oh yes, copy/paste typo :slight_smile:


API 1.0:

atom.commands.add 'atom-text-editor',
  'editor:toggle-current-row-folding': (event) ->
    editor = @getModel()
    bufferRow = editor.bufferPositionForScreenPosition(editor.getCursorScreenPosition()).row
    if editor.isFoldedAtBufferRow(bufferRow)

Single hotkey for toggling code folding?
How can I use the same shortcut for Fold and Unfold?

Ah, I got mine to work again with atom-workspace, but this is much better! :smiley:


Brilliant, thank you.


Doesn’t seem to work on my 1.2.4, what am I missing?


Finally occurred to me what I was missing, forgot to keymap to this custom function. Adding here in case others blanked on it too:

  1. Add script (from @kshaper above) to your Init Script
  2. Add this to your Keymap: 'f1': 'editor:toggle-current-row-folding'

This replicates the fold/unfold behavior of Textmate.


This is a wonderful solution, however it doesn’t work for selecting multiple lines. I’m coming from Komodo Edit where you can select multiple lines and toggle the fold with a single keybinding. Can this function be improved to also work for multiple line selections?


Can we make a similar function for toggle-fold-all?


Here’s a similar version to toggle all foldings. In

allFolded = false
atom.commands.add 'atom-text-editor',
  'editor:toggle-current-folding': (event) ->
    editor = @getModel()
    bufferRow = editor.bufferPositionForScreenPosition(editor.getCursorScreenPosition()).row
    if allFolded
      allFolded = false
      allFolded = true

In keymap.cson:

  'cmd-alt-0': 'editor:toggle-current-folding'

or whatever you want.