0

How to configure Emacs to run code in different languages as shown in this post? Specifically, how to run these type of code blocks #+begin_src shell :results list ?

After 9 years of vim, that post inspired me to try Emacs. What attracts me is the ability to run a wide range of code within Emacs. I successfully configured Emacs to run in Evil mode with the keybinding I prefer, but I still have difficulties to find how to make it work as demonstrated in the post.

  • Sure, the question should be specific. When you are a beginner -- formulating the right question is the majority of the effort. The responses below are exactly what I needed. – ashim Apr 28 at 21:51
  • Updated the question. – ashim Apr 28 at 23:32
3

The example you pointed to (as you probably already know) require a certain amount of configuration, and also require understanding of certain concept in order to figure out what is going on.

I'm going to execute a trivial shell code block in an emacs after ensuring that I am not relying on any of my personal emacs configuration

Concepts:

  • Skip emacs configuration: run emacs as "emacs -q bar.org"
  • Org mode buffer - most emacs configs will auto-assign the editing behaviour for a buffer based on the file extension. So the fact that the file I visit is ends in ".org" is critical.
  • Org babel - this is the function (which allows execution of an embedded code block) that we are using. The manual is at https://orgmode.org/worg/org-contrib/babel/intro.html#fn.1

Steps:

  1. emacs -q bar.org
  2. Enable handling of shell blocks by executing a lisp expression:
    • Type in (org-babel-do-load-languages 'org-babel-load-languages '((shell . t)))
    • Execute this block by type C-x C-e or M-x eval-last-sexp
  3. Add example shell call:

    #+begin_src shell :results list
    ls /etc/pam.d
    #+end_src

  4. Execute this block. Use the key sequence C-c C-c for this.

Result:

If you are on a linux (or maybe mac), you should see a list of the contents of a directory.

| improve this answer | |
  • Thank you for steps that are easy to reproduce. – ashim Apr 28 at 21:48
2

Welcome to emacs! I recommend you to check the official org documentation about the evaluation of code blocks here.

So, the thing to use is org-babel, to evaluate the source code inside org-mode blocks, babel has a default list of languages that can be used out of the box.

If one on the languages is not in there, you can try looking for it in MELPA or in ELPA, for example, the ipython babel is on MELPA.

So the quick answer is, define the code blocks and evaluate the buffer with:

M-x org-babel-execute-buffer

You can do that by typing C-c C-c anywhere within the code block. For example:

#+BEGIN_SRC sh :results output
echo "hi"
#+END_SRC


#+BEGIN_SRC emacs-lisp :results output
(print "hello again")
#+END_SRC


If the command is successful, there will be a #+RESULTS: block with the actual result after the evaluated code block.

Good luck!

| improve this answer | |
  • 1
    ob-python.el is part of Org mode (and part of emacs). You do not have to get it from MELPA or ELPA. And I should point out that there is no substitute to reading the Org mode documentation (not just the part about "Working with source code"). – NickD Apr 28 at 21:34
  • Right! I was thinking of the ob-ipython package with jupyter, my bad – f-sasa Apr 28 at 21:37
  • Thank you for your answer and the useful links within! – ashim Apr 28 at 21:50

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.