Emacs: Org Mode, Programing Language Code Markup

By Xah Lee. Date: . Last updated: .

Emacs org-mode allows you embed language source code in the file, and you can interactively evaluate it, edit it, or export to HTML with evaluated results embedded/updated in the exported file.

emacs org mode babel 2015-02-21
Emacs Org mode Babel feature.

If you've never used org-mode before, first see Emacs: Writing Outline, org-mode Tutorial

Source Code Marked Up

Source code in org mode is marked up like this:

#+BEGIN_SRC emacs-lisp
(+ 1 3)
#+END_SRC

Here's example of other languages:

#+BEGIN_SRC ruby
p 3 + 4
#+END_SRC
#+BEGIN_SRC perl
print 3 + 4
#+END_SRC

Key Shortcut to Insert Source Code Markup

To insert source code markup, press 【< s Tab】.

Then, to insert your language's name, Alt+x complete-symbolCtrl+Alt+i

Supported languages

The following are the keywords for supported languages, as of 2015-02-20

  1. asymptote
  2. awk
  3. calc
  4. C
  5. C++
  6. clojure
  7. css
  8. ditaa
  9. dot
  10. emacs-lisp
  11. gnuplot
  12. haskell
  13. java
  14. js
  15. latex
  16. ledger
  17. lisp
  18. lilypond
  19. matlab
  20. mscgen
  21. ocaml
  22. octave
  23. org
  24. oz
  25. perl
  26. plantuml
  27. python
  28. R
  29. ruby
  30. sass
  31. scheme
  32. screen
  33. sh
  34. sql
  35. sqlite

Evaluate Code

To evaluate code block, put cursor in the code block, then press 【Ctrl+c Ctrl+c】.

Here's a example you can copy paste into org mode file to test.

#+BEGIN_SRC emacs-lisp
(+ 3 4)
#+END_SRC

#+RESULTS:
: 7

Setup Which Languages to Allow Eval

By default, only emacs lisp code is allowed to eval. If you need other languages, you need to add this to your emacs init:

(require 'org)
(require 'ob)

;; make org mode allow eval of some langs
(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t)
   (clojure . t)
   (python . t)
   (ruby . t)))

Restart emacs, or, select the code and Alt+x eval-region.

Or, you can put the elisp code in the beginning of org file, and tell user to press 【Ctrl+c Ctrl+c】 to eval it.
#+BEGIN_SRC emacs-lisp
;; make org mode allow eval of some langs
(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t)
   (clojure . t)
   (python . t)
   (ruby . t)))
#+END_SRC

Here's a example of Python. Try to eval it.

#+BEGIN_SRC python
return 3 + 7
#+END_SRC

#+RESULTS:
: 10

Note: for Python, when in non-session mode, you need to have a return … at the end, in order to print result, else the result is None.

Stop Emacs from Confirming Eval

Everytime you eval a source code in org mode, emacs will ask you to confirm.

You can turn that off by putting the following in your emacs init.

;; stop emacs asking for confirmation
(setq org-confirm-babel-evaluate nil)

Or, if you want it to be per buffer, you can put the following at the top of your org file, and eval it everytime you open that file.

#+BEGIN_SRC emacs-lisp
;; stop emacs asking for confirmation, for this buffer only
(setq-local org-confirm-babel-evaluate nil)
#+END_SRC

Turn on Syntax Coloring

To make org mode syntax color embeded source code, put the following in your emacs init:

(setq org-src-fontify-natively t)

Editing Source Code

• When cursor is inside the source code block, Alt+x org-edit-specialCtrl+c '】 to create a new buffer in split pane, dedicated to the language.

• edit it, and Alt+x org-edit-src-saveCtrl+x Ctrl+s】 will save it back in the original org mode file.

• call org-edit-specialCtrl+c '】 again to remove this edit mode buffer.

Reference

http://orgmode.org/manual/Working-With-Source-Code.html#Working-With-Source-Code

Org mode's source code feature (originally known as Org Babel) is developed by Eric Schulte. Thank you Eric Schulte.

Eric's home page is http://www.cs.unm.edu/~eschulte/

Eric Schulte is also the author of emacs starter kit. http://eschulte.github.io/emacs24-starter-kit/

org mode Topic

  1. Emacs: Org Mode Markup Cheatsheet
  2. Emacs: Writing Outline, org-mode Tutorial
  3. Emacs: Org Mode, Programing Language Code Markup
  4. Emacs: Using Org Mode for Todo
  5. Emacs: Org Mode Customization
Like it? Buy Xah Emacs Tutorial. Thanks.