1

When I do $ tree -d -L 3 /usr/share/emacs/28.2/lisp in a bash terminal I get:

/usr/share/emacs/28.2/lisp
├── calc
├── calendar
├── cedet
│   ├── ede
│   ├── semantic
│   │   ├── analyze
│   │   ├── bovine
│   │   ├── decorate
│   │   ├── symref
│   │   └── wisent
│   └── srecode
...

But when I execute this block from an org-file:

#+begin_src sh 
tree -d -L 3 /usr/share/emacs/28.2/lisp
#+end_src

I have the following results:

#+RESULTS:
| /usr/share/emacs/28.2/lisp |               |          |          |
| ├──                        | calc          |          |          |
| ├──                        | calendar      |          |          |
| ├──                        | cedet         |          |          |
| │                          | ├──           | ede      |          |
| │                          | ├──           | semantic |          |
| │                          | │             | ├──      | analyze  |
| │                          | │             | ├──      | bovine   |
| │                          | │             | ├──      | decorate |
| │                          | │             | ├──      | symref   |
| │                          | │             | └──      | wisent   |
| │                          | └──           | srecode  |          |
...

Is it possible to have the results printed properly (i.e. as they look in my terminal)?

1 Answer 1

3

Add :results output like that:

#+begin_src sh :results output
tree -d -L 3 /usr/share/emacs/28.2/lisp
#+end_src

This overrides the default :results table setting and wraps the results (assuming they are not too short - see below) in an EXAMPLE block.

The only potential disadvantage is that if the output is shorter than 10 lines[1], then it is not wrapped - instead, it is printed as literal output, prepending : to each line.

There are a few ways to treat both short and long output uniformly:

  • instead of using :results output, one could use :results output :wrap example which always wraps the output in an EXAMPLE block, regardless of its length
  • one could use :results drawer which wraps the output inside a :results drawer, again regardless of its length
  • one could customize the variable org-babel-min-lines-for-block-output which defines the boundary between what is considered short and long output and whose default value is 10. If it is customized to 0, then :results output always wraps the results in an EXAMPLE block.

[1] That's the default value of org-babel-min-lines-for-block-output which is described towards the end of the answer.

6
  • If I copy the code above into an Emacs buffer (file), how can I run the tree command and I see its output?
    – oOosys
    Commented Apr 18, 2023 at 23:31
  • 1
    The question and the answer are related to org files. So open an org file eg. C-x C-f test.org RET, copy paste the block and do C-c C-c inside it.
    – crocefisso
    Commented Apr 19, 2023 at 0:04
  • Bash has to be activated in your init though. Read "Requirements and Setup" section from here. Also, tree CLI has to be installed on your system.
    – crocefisso
    Commented Apr 19, 2023 at 0:12
  • 1
    I have added the contents of my previous comment into the answer, thereby making it as complete as I can make it. I hope you find the additions worthwhile. If not, feel free to revert.
    – NickD
    Commented Apr 19, 2023 at 2:35
  • 1
    Yeah, I certainly didn't want to go into all the header arguments: I just wanted to cover a few of the possibilities that were relevant to your question. The rest are covered in the documentation: we don't want to duplicate that on Emacs SE. But if there are specific questions that can be answered here, we'll deal with them at that time.
    – NickD
    Commented Apr 19, 2023 at 13:09

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.