0

Is there an easy method to limit the number of lines in org-babel results?

For example: I have the following org-babel block:

#+begin_src js :exports both :results output
for (let i = 1; i <= 100; i++) {
  if (i % 3 === 0 && i % 5 === 0) {
    console.log("FizzBuzz");
  } else if (i % 3 === 0) {
    console.log("Fizz");
  } else if (i % 5 === 0){
    console.log("Buzz");
  } else {
    console.log(i);
  }
}
#+end_src

This creates the following output:

#+RESULTS:
#+begin_example
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
#+end_example

Is there an easy way to limit this long output to just 10 lines and add "..." at the end? Thanks.

1 Answer 1

2

This is a literate org-mode answer. Copy the whole answer into an Emacs buffer, run M-x org-mode and experiment with the code blocks.

You can use a [[info:org#Results of Evaluation][:post]] processing step to limit the number of lines (or list items).

The following emacs-lisp snippet takes either a string or a sequence. If DATA is a string, it will split DATA by newlines, take LINES lines and glue them together with SEP.

If DATA is a sequence, then the first LINES elements get returned instead. This enables you to use =:post= with =:result= being =list= or a newline separated string.

#+NAME: head
#+begin_src emacs-lisp :var data="" :var sep="\n" :var lines=10 :exports code
  (cond
   ((stringp data) (mapconcat #'identity (seq-take (split-string data "[\n]") lines) sep))
   ((seqp data)    (seq-take data lines)))
#+end_src

Here's an example that creates a FizzBuzz string, separated by newline. Try to change the =:results= field for some experimentation:

#+begin_src emacs-lisp :exports both :wrap example :results value :post head(*this*,lines=20)
  (defun fizzbuzz (number)
    (cond ((= 0 (% number 15)) "FizzBuzz")
      ((= 0 (% number 5)) "Buzz")
      ((= 0 (% number 3)) "Fizz")
      (t (number-to-string number))))
  (mapconcat 'fizzbuzz (number-sequence 1 100) "\n")
#+end_src

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.