0

I would like to evaluate the code blocks in org-mode. However, when the output is lengthy, I want the output result to be collapsed. When I export the org file to the html, I want to see the result. Therefore, I do not want the option none or silent. I just want to collapse the output by default.

#+begin_src shell :results output
  grep cat$ /usr/share/dict/words
#+end_src

This is the output.

 #+RESULTS:
  #+begin_example
  bobcat
  cat
  Chilcat
  copycat
  crazycat
  ducat
  gaycat
  hellcat
  hellicat
  hepcat
  kitcat
  Magnificat
  muscat
  polecat
  pulicat
  pusscat
  pussycat
  saltcat
  scat
  scratchcat
  snobscat
  supplicat
  tearcat
  tipcat
  tomcat
  wheencat
  whipcat
  wildcat
  wullcat
  #+end_example

What I want is the result looks like:

  #+RESULTS:
  #+begin_example...

When I reopen the org file, only the results section will be shown as collapsed. How can I achieve this?

1 Answer 1

1

Assuming you don't mind hiding the whole #+RESULTS: block, instead of hiding the #+example block underneath, you can add this:

(with-eval-after-load 'ob-core
  (add-hook 'org-babel-after-execute-hook #'org-babel-result-hide-all)
  (add-hook 'org-mode-hook #'org-babel-result-hide-all))

to your init file. Running the first hook will hide all #+RESULTS: blocks in the buffer: that happens after every evaluation of a code block. Running the second hook does the same thing, but happens when you open the file.

Depending on your setting, you may need to add this to the top of your file in order to have everything unfolded to begin with (except for the #+RESULTS: blocks):

#+STARTUP: showeverything

The mode hook takes care of hiding all the #+RESULTS: blocks afterwards.

6
  • Did this work for you?
    – NickD
    Aug 6, 2023 at 18:48
  • Unfortunately, this did not work for me. I am on version 29.1 btw. Aug 7, 2023 at 9:44
  • Strange: does it work if you start with emacs -q and a minimal init file? If it does, then there's probably some setting in your actual init file that interferes. Can you describe what happens when you evaluate a code block? Are the results still visible? Check the value of the hook with C-h v org-babel-execute-hook to make sure that it includes the org-babel-result-hide-all function.
    – NickD
    Aug 7, 2023 at 12:25
  • 1
    org-babel-after-execute-hook functions value is ok. When I tried this with minimal config it worked. Trying to figure out the problem in my init file. Currently, M-x org-babel-result-hide-all function does not work in my org file also. I will edit this once I figure out a solution for my case. Aug 7, 2023 at 15:42
  • "Currently, M-x org-babel-result-hide-all function does not work in my org file also." - OK, this seems to be the underlying problem. I'll take a look later on and see if I can spot anything.
    – NickD
    Aug 7, 2023 at 15:48

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.