Sometimes you accidentally evaluate your entire Org-Mode document resulting in result blocks everywhere. Maybe you can’t easily revert the change so you are stuck with a ton of code you don’t need. Here is a function to remove all of your result blocks. It is pretty good for documents that you probably never wanted to evaluate in the first place:
(defconst help/org-special-pre "^\s*#[+]") (defun help/org-2every-src-block (fn) "Visit every Source-Block and evaluate `FN'." (interactive) (save-excursion (goto-char (point-min)) (let ((case-fold-search t)) (while (re-search-forward (concat help/org-special-pre "BEGIN_SRC") nil t) (let ((element (org-element-at-point))) (when (eq (org-element-type element) 'src-block) (funcall fn element))))) (save-buffer))) (define-key org-mode-map (kbd "s-]") (lambda () (interactive) (help/org-2every-src-block 'org-babel-remove-result)))
12 thoughts on “Remove Every Source Block Results”
did you know that something similar (or exactly the same is in the core)?
It is org-babel-remove-result-one-or-many
The missing one is removing all results bodies while retaining the result placeholder.
C-u org-babel-remove-result operates only on one block.
I didn’t know that. Thank you for pointing it out.
@not sure org-babel-remove-result-one-or-many removes current or all result blocks in the buffer.
Grant’s solution removes all result blocks after current point.
I needed a similar solution but limited to current or parent subtree so I ended up with:
Thank you Daniel. FWI I wrapped your code in an HTML pre block.
This looks exactly like what I need, but I’m a fairly new emacs user and I’m confused. I copied the function to my init.el but I don’t know how to invoke it. Thanks.
and then type
and hit enter.
Thanks for this!
I use Spacemacs and wanted to call your function with
M-x org-babel-remove-results, so I added this to my
It seems to work. Hopefully this is helpful for any other Emacs/Spacemacs newbies like me.
Also just to remark that Daniel B commented that the function removes all source blocks “after current point”, but I see it removing them globally in the file. Presumably that’s what
goto-char (point-min)does? I’m not dealing with subtrees in the org file I’m using your code.
I realised the function doesn’t remove results from
#+CALL:blocks so I adapted it to do that as well:
(I wasn’t sure how to format the code in WordPress hence the gist, sorry!)
Than you Daniel.
In WordPress you can wrap code in a
preblock to get the formatting you expect.
I just edited your comment to do so.
Presumably that’s what goto-char (point-min) does?