0

In org-mode, how could one specify the visibility state of individual SRC_BLOCKs when a file is opened?

org-mode provides the variable org-hide-block-startup to specify if all source blocks are shown or hidden when a file is opened. How could I specify, instead, the initial state of individual source blocks when a file is opened?

The visibility state of headings can be manually chosen using the VISIBILITY property. It is not obvious, however, if there is something similar for source blocks.

1

Indeed, some code blocks are too long and take up too much space.

By default, you want to display all the code blocks, only hidden when you set a block of code to hide.

Suppose there is a block of code in an org file that needs to be hidden, like:

#+BEGIN_SRC emacs-lisp :hidden
(defun individual-visibility-source-blocks ()
  "Fold some blocks in the current buffer."
  (interactive)
  (org-show-block-all)
  (org-block-map
   (lambda ()
     (let ((case-fold-search t))
       (when (and
              (save-excursion
                (beginning-of-line 1)
                (looking-at org-block-regexp))
              (cl-assoc
               ':hidden
               (cl-third
                (org-babel-get-src-block-info))))
         (org-hide-block-toggle))))))

(add-hook
 'org-mode-hook
 (function individual-visibility-source-blocks))
#+END_SRC

When the command "individual-visibility-source-blocks" is used, all code blocks are displayed except for the code block.

  • Your answer is nice and clever. You've made me aware of the function org-block-map which I'm sure will me put to use in many other places. – UndeadKernel Sep 24 '18 at 15:22

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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