8

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.

AnoE
  • 356
  • 1
  • 11

3 Answers3

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
  :demand
  :config
  (define-prefix-command 'origami-mode-map)
  (define-key ctl-x-map (kbd "z") 'origami-mode-map)
  (global-origami-mode)
  :bind
  (: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.

GergelyPolonkai
  • 748
  • 5
  • 12
  • 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 Jun 13 '22 at 07: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. – GergelyPolonkai Jun 13 '22 at 08:33
2

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

https://www.gnu.org/software/emacs/manual/html_node/emacs/Selective-Display.html

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

Marek L
  • 21
  • 1
  • 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 Jun 13 '22 at 07:52
-1

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.

Phil Hudson
  • 1,651
  • 10
  • 13