0

I have a named org-babel source block like this:

#+NAME: my-org-babel-block
#+BEGIN_SRC elisp :results output
(print org-babel-load-languages)
#+END_SRC

Is there any way to call this block with M-x my-org-babel-block?

1 Answer 1

1

No, but you can do something like this:

#+begin_src emacs-lisp
  (defun my-org-babel-block ()
    (interactive)
    (org-sbe "my-org-babel-block"))
#+end_src

defining a command (i.e. a function with an interactive spec) that executes the block with the given name. You can now call the command with M-x my-org-babel-block which will use org-sbe to execute the block.

The doc string of org-sbe (which you can get with C-h f org-sbe RET) says in part:

org-sbe is a Lisp macro in ‘ob-table.el’.

(org-sbe SOURCE-BLOCK &rest VARIABLES)

Return the results of calling SOURCE-BLOCK with VARIABLES.

Each element of VARIABLES should be a list of two elements: the first element is the name of the variable and second element is a string of its value.

6
  • Does this work with other languages than emacs lisp? Dec 7, 2020 at 20:53
  • 1
    Yes: org-sbe executes a code block - it doesn't care what language the code block is in.
    – NickD
    Dec 7, 2020 at 21:07
  • Right, you're evaluating the emacs-lisp block there and it refers to the other block named my-org-babel-block. Dec 7, 2020 at 21:52
  • With this, I have to define one function per named block. Is it possible to define a function that does this for all named blocks in the file? Dec 7, 2020 at 21:53
  • Yes, but that is a different question which you should ask separately. And when you do, specify what you mean for the function to do: I can think of two different interpretations of the question.
    – NickD
    Dec 7, 2020 at 22:05

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.