When working with YAML (using yaml-mode), is there a way to fold a hierarchical tree in a DWIM manner (i.e., without manually adding the {{{ ... }}} markers of folding-mode manually)?

yaml-mode itself seems to have no provisions for that.

The solution must religiously keep the indentation intact, as this is critical to YAML.


3 Answers 3


As it seems, no builtin solution exists for your problem.

Origami (available from MELPA) can do it for you, although it does some strange things for arrays of objects:

- prop1: Value 1
  prop2: Value 2...

Here, the ellipsis is Origami’s marker to show there’s hidden text. This is probably because, as the README says:

Anything not in this list [of supported modes] will be folded using indentation. This works surprisingly well for most major-modes and is great for folding text.

Origami, however, doesn’t have hardcoded key bindings, so here’s how i install it (YMMV):

(use-package origami
  (define-prefix-command 'origami-mode-map)
  (define-key ctl-x-map (kbd "z") 'origami-mode-map)
  (:map origami-mode-map
   ("o" . origami-open-node)
   ("O" . origami-open-node-recursively)
   ("c" . origami-close-node)
   ("C" . origami-close-node-recursively)
   ("a" . origami-toggle-node)
   ("A" . origami-recursively-toggle-node)
   ("R" . origami-open-all-nodes)
   ("M" . origami-close-all-nodes)
   ("v" . origami-show-only-node)
   ("k" . origami-previous-fold)
   ("j" . origami-forward-fold)
   ("x" . origami-reset)))

This makes C-x z c close a node, and C-x z o open it again.

  • Just fyi - at the point of this answer, and still today, I do not find a package origami in my package list (which includes elpa.gnu.org and stable.melpa.org amongst others). GNU Emacs 27.2. There are other packages with "origami" in their name, which seem to be adding more functionality around it, but they are all listed with the "incompatible" tag.
    – AnoE
    Commented Jun 13, 2022 at 7:51
  • Indeed; origami seems to be available only from non-stable MELPA or their repo. They have an issue open for this since 2016 without a reply which is sad. Commented Jun 13, 2022 at 8:33

As yaml is indentation based for folding nested blocks can be used native function set-selective-display


Example: C-u 4 C-x $ hides yaml file lines that starts with 4 or more spaces.

  • This is a nice start! One could imagine creating a little function which automatically uses the current position of the cursor as default indentation if the selective display is currently not active, to make it even more useful...
    – AnoE
    Commented Jun 13, 2022 at 7:52

Try enabling hideshow minor mode:

M-x hs-minor-mode

Assuming yaml-mode is conventionally written, you should then be able to toggle folding of a (sub)tree, type C-c @ C-c.

Type M-: (info "(emacs) Hideshow") RET to go to the manual section.

  • 5
    As it seems, unfortunately it doesn’t. Commented Apr 21, 2021 at 4:28
  • Aye, it doesn't (hs-toggle-hiding does nothing, and also gives no error message). Seems like yaml-mode is not conventional...
    – AnoE
    Commented May 24, 2023 at 10:46

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.