15

When I open an org file, I want all the src blocks to start collapsed and stay that way until I explicitly org-cycle them.

I can't find any variable to control this behavior. Perhaps I'm overlooking something? Or maybe someone has an elisp solution?

1
  • 4
    Here is a link to a related questions that talks about using the variable org-hide-block-startup: stackoverflow.com/a/21563210/2112489. And, the following link has something to do with cycling visibility of src blocks that I wrote up a while back in time, but it doesn't set them to be hidden by default. I won't have time to work on this today or tomorrow, but here is the link in case you or anyone else wants to play a bit with the code: stackoverflow.com/a/25496350/2112489
    – lawlist
    Jan 9, 2015 at 15:10

1 Answer 1

15

You can achieve what you want by adding the org-hide-block-all command to org-mode-hook:

(add-hook 'org-mode-hook 'org-hide-block-all)

For my personal configuration, I've extended this solution a bit:

(defvar org-blocks-hidden nil)

(defun org-toggle-blocks ()
  (interactive)
  (if org-blocks-hidden
      (org-show-block-all)
    (org-hide-block-all))
  (setq-local org-blocks-hidden (not org-blocks-hidden)))

(add-hook 'org-mode-hook 'org-toggle-blocks)

(define-key org-mode-map (kbd "C-c t") 'org-toggle-blocks)

With this in place, org-mode collapses SRC blocks by default and lets me toggle visibility of all SRC blocks by hitting C-c t. (If you decide to use this code you can of course adapt the key binding to your liking.)

1
  • i'm looking for a solution that read a metadata and collapses or not by default if metadata says so Jul 17, 2022 at 16:04

Your Answer

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

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