How can I run the test src block without leaving the code src block?

Currently I must leave code, goto test, hit C-c C-c , go back to continue work on code

#+NAME: code
#+BEGIN_SRC sh :tangle /tmp/foo.sh

#+NAME: test
#+BEGIN_SRC sh :var DUMMY=(progn (org-babel-goto-named-src-block "code") (org-babel-tangle '(4))) :results output
echo "Process tests on this file":
cat /tmp/foo.sh

: Process tests on this file:
: foo="bar"

Using the promising looking #+begin_src sh :tangle /tmp/foo.sh :var DUMMY=(progn (org-babel-goto-named-src-block "test") (org-babel-execute-src-block)) on the code src block returns Lisp nesting exceeds ‘max-lisp-eval-depth’

Org mode version 9.4.5

  • with org 9.4.6 your code works fine for me. Jul 31, 2021 at 10:22
  • @JohnKitchin John you mean you're able to hit C-c C-C in the code block to tangle itself to foo.sh and test is run subsequently? Offtopic: I read your org mode docstring testing blogpost since this current question relates to pytest with org babel
    – jjk
    Jul 31, 2021 at 10:29
  • Yes, CcCc in the test block tangles the “code” block and runs the tangled file for me. Jul 31, 2021 at 10:32
  • I must remain on the current line in the code block. No back and forth switching to the the test block.
    – jjk
    Jul 31, 2021 at 10:35
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Jul 31, 2021 at 23:24

1 Answer 1


Don't do it with babel: do it in lisp - define a function to do what you want and bind it to a key. You can then execute the function from anywhere using that key. Something like this:

#+begin_src elisp
(defun ndk/org-babel-evaluate-test-block-from-code-block ()
    (org-babel-goto-named-src-block "code")
    (org-babel-goto-named-src-block "test")

(define-key org-mode-map (kbd "C-c z") #'ndk/org-babel-evaluate-test-block-from-code-block)

Pressing C-c z from anywhere in the buffer will do the evaluations and leave point unchanged. If you do it from the code block, that's where you will remain.

  • 1
    It probably doesn't hurt but I don't think the nested save-excursion is needed: I should have removed it when I modified the function to work from anywhere without moving point. I'll test without it and edit the answer.
    – NickD
    Jul 31, 2021 at 18:24
  • 1
    Tested and edited the answer to remove the nested save-excursion.
    – NickD
    Jul 31, 2021 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.