Over at the Emacs subreddit, justrajdeep asks an interesting question: If you have several Babel source blocks in an Org document, is there any way to execute all the blocks and get their data with a single command.
I often have this problem when I have a document with two or more Babel blocks and want to export it. On export, every to block will ask me if I want to execute it. It’s not a huge problem but it is a bit annoying. My go to solution is to set org-confirm-babel-evaluate
to nil temporally. After a while, I wrote a function to handle this automatically.
Karthink to the rescue. There’s a command, org-babel-execute-buffer
, to execute every Babel buffer in an Org file. If you run it, every Babel buffer in the Org file is executed. Unfortunately, it still asks for confirmation before executing each block.
That’s easily fixed with a bit of Elisp. Here’s some code I whipped up in less than a minute:
(defun jcs-execute-all-buffers () (interactive) (let ((org-confirm-babel-evaluate nil)) (org-babel-execute-buffer)))
That code will execute all Babel buffers in the file without asking for confirmation. If you just want an easy way of executing all the blocks at one time and don’t mind authorizing each execution, then org-babel-execute-buffer
is all you need. If you don’t want to have to okay each block, a bit of Elisp such as the above is all you need.