I found a snippet that let's me fold bad(large) blocks of code

(add-hook 'ruby-mode-hook
          (lambda () (hs-minor-mode)))

(eval-after-load "hideshow"
  '(add-to-list 'hs-special-modes-alist
                  ,(rx (or "def" "class" "module" "do" "{" "[")) ; Block start                                   
                  ,(rx (or "}" "]" "end"))                       ; Block end                                     
                  ,(rx (or "#" "=begin"))                        ; Comment start                                 
                  ruby-forward-sexp nil)))

(global-set-key (kbd "C-c h") 'hs-hide-block)
(global-set-key (kbd "C-c s") 'hs-show-block)

Unfortunately, this code is not smart enough.


What are my alternatives?

Update: Noticed sublime text does it out of the box

enter image description here

1 Answer 1


I tried yafolding, seems to work (theres room for improvement on this, though)

(require 'yafolding)
(add-hook 'ruby-mode-hook 'yafolding-mode)

(global-set-key (kbd "M-RET")   'yafolding-toggle-element)

Possible alternatives: "yafolding", "hideshow", "folding.el", "imenu.el","vimish-fold"

Here's a how yafolding folded blocks look like:

enter image description here

  • 1
    works well for folding/unfolding elixir code blocks in elixir-mode too, thanks!
    – ashraf
    Dec 21, 2018 at 15:25
  • 1
    I've tried a bunch and yafolding is the first to support Ruby! Very Nice! Apr 25, 2020 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.