Category Archives: org

Org-mode basics VI: A simple TODO list

In the first part of my series on org-mode, I described how to create a rich structured notebook that can be exported to various useful formats. In the next few posts in this series I’m going to talk about another essential way I use org-mode, which is to organise myself! I use org-mode to manage my (depressingly long) task list including scheduled tasks and deadlines, to export a calendar feed for events, and to quickly capture useful information including emails and scanned documents. If you want to see a very advanced use case, look at Bernt Hansen’s Org Mode – Organize Your Life In Plain Text.

In this post, we’ll start by looking at setting up a simple todo list, and we’ll cover some of the more advanced topics later.

To start with, add the following code to your emacs config file:

;; set key for agenda
(global-set-key (kbd "C-c a") 'org-agenda)

;;file to save todo items
(setq org-agenda-files (quote ("/Users/bjm/")))

;;set priority range from A to C with default A
(setq org-highest-priority ?A)
(setq org-lowest-priority ?C)
(setq org-default-priority ?A)

;;set colours for priorities
(setq org-priority-faces '((?A . (:foreground "#F0DFAF" :weight bold))
                           (?B . (:foreground "LightSteelBlue"))
                           (?C . (:foreground "OliveDrab"))))

;;open agenda in current window
(setq org-agenda-window-setup (quote current-window))

;;capture todo items using C-c c t
(define-key global-map (kbd "C-c c") 'org-capture)
(setq org-capture-templates
      '(("t" "todo" entry (file+headline "/Users/bjm/" "Tasks")
         "* TODO [#A] %?")))

This sets up the file in which we will save our todo items (put your own choice of file here), configures a few other options, and sets up a capture template to quickly add todo items to the list (put the same file name for your todo file here too). Now you can highlight the code in your config file and use M-x eval-region or restart emacs to pick up the changes.

Once you have done this you can add your first todo item using C-c c t, which will pop up a small window with a prompt like this

** TODO [#A]

You can see this looks like an org-mode headline, and you should add your todo item and any notes to go with is to this like so:

** TODO [#A] make a todo list
Some notes here about how to do it

Then hit C-c C-c to save this item. The nice thing about this method of adding items (called org-capture) is that you can add an item from anywhere in emacs and get right back to what you were doing afterwards.

By default our item was given priority A, but you can change this easily by hitting shift and up or down arrow to cycle through the priority levels (I find that three levels, A-C is enough for me).

Let’s add a priority B item:

** TODO [#B] add another item to my list

Now we can have a look at our todo list using C-c a to launch the “agenda dispatcher”, a powerful interface for selecting different ways to view your tasks. For now we’ll just hit t in the dispatcher to view the todo items (i.e. use C-c a t). This switches to a buffer with our todo list – in this list view, you might want to:

  • Cross an item off your list (the best bit!). To do this put the cursor on the corresponding line and hit $ which marks it as done and archives the item in a file called todo.org_archive getting rid of it from your todo list.
  • Change the priority of an item using shift up/down.
  • View the notes to go with items by hitting E.
  • Edit or view an item in more detail by hitting RET with the cursor on the item that you want. This takes you to the item in your file where you can edit it or look at the notes you added to it in more detail.
  • Quit back to where you were before with q

That is all there is to it, and you now have a simple but powerful todo list in emacs. Just remember C-c c t to create a todo item and C-c a t to view the todo list.

That’s all for now. In the next part we’ll look at scheduling and deadlines.

Org-mode basics V: Exporting your notes

In this final post of my short series on using org-mode to write rich, structured notes, we will look at exporting the notes as a web page or pdf document. The previous posts covered structuring your notes and adding tables and links and images, and formatting text and source code.

If you have been following along, you should have an org file containing all of the notes on org-mode from the previous posts. We’ll now look at exporting that file.

One strength of org-mode is the ability to export to multiple formats. Probably the most useful to begin with are web pages and pdf (via latex) but more are available; to quote the org manual

ASCII export produces a readable and simple version of an Org file for printing and sharing notes. HTML export allows you to easily publish notes on the web, or to build full-fledged websites. LaTeX export lets you use Org mode and its structured editing functions to create arbitrarily complex LaTeX files for any kind of document. OpenDocument Text (ODT) export allows seamless collaboration across organizational boundaries. Markdown export lets you seamlessly collaborate with other developers. Finally, iCal export can extract entries with deadlines or appointments to produce a file in the iCalendar format.

To export your org file to a web page, type C-c C-e to start the exporter and then press h to select html and o to select open. A new web page should now open in your browser.

Similarly, typing l and o in the exporter will convert the org file to latex and then compile it to produce a pdf and display that. Try both of these.

It is possible to add many customisations to the export process. For example, go to the top of the buffer (using M-<) and use C-c C-e and then # to insert an export template. You can then choose to add html or latex (or other) templates (press TAB to see the list).

As an example, add the following to the top of your org file to tweak the appearance of the exported documents.

#+LaTeX_CLASS: bjmarticle
#+TITLE:     Org-mode Basics
#+AUTHOR: Ben Maughan
#+OPTIONS: html-link-use-abs-url:nil html-postamble:auto
#+OPTIONS: html-preamble:t html-scripts:t html-style:t
#+OPTIONS: html5-fancy:nil tex:t
#+HTML_DOCTYPE: xhtml-strict
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="" />
#+CREATOR: <a href="">Emacs</a> 24.4.1 (<a href="">Org</a> mode 8.3.2)

This is the default html export template with a couple of tweaks.

  • I have added a link to a style sheet to style the html
  • I have added a latex class bjmarticle to control the appearance of the generated pdf

The latex class is defined in my emacs config file with the following

(add-to-list 'org-latex-classes
\\geometry{a4paper,left=2.5cm,top=2cm,right=2.5cm,bottom=2cm,marginparsep=7pt, marginparwidth=.6in}"
               ("\\section{%s}" . "\\section*{%s}")
               ("\\subsection{%s}" . "\\subsection*{%s}")
               ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
               ("\\paragraph{%s}" . "\\paragraph*{%s}")
               ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

You’ll need some experience of LaTeX to make significant changes here, but the sky is the limit.

I have compiled the series of posts on org-mode basics into a single org file, and exported it with this set of export options.

  • The org file is here
  • The exported web page is here
  • The exported pdf is here

The wrapping of the example and code blocks in the pdf needs to be fixed, but overall we get some pretty nice looking documents with minimal effort.

Wrap text in an org-mode block

In my most recent post on org-mode, I talked about using blocks to mark text as being latex, or source code and so on. I mentioned using the shortcuts like <e then TAB on a new line to create an empty block. Sometimes it is handy to wrap existing text in a block, and the following function does that for the text you have selected.

This has been in my config file for ages and I can’t remember where it came from – I know I didn’t write it! A bit of googleing suggests this could be the origin, but if anyone knows different, let me know.

I bind the function to C-< because it reminds me of the < shortcuts to create the blocks, and I don’t use the org-cycle-agenda-files that is usually bound to that key combo.

;; function to wrap blocks of text in org templates                       ;;
;; e.g. latex or src etc                                                  ;;
(defun org-begin-template ()
  "Make a template at point."
  (if (org-at-table-p)
      (call-interactively 'org-table-rotate-recalc-marks)
    (let* ((choices '(("s" . "SRC")
                      ("e" . "EXAMPLE")
                      ("q" . "QUOTE")
                      ("v" . "VERSE")
                      ("c" . "CENTER")
                      ("l" . "LaTeX")
                      ("h" . "HTML")
                      ("a" . "ASCII")))
               (concat (propertize "Template type: " 'face 'minibuffer-prompt)
                       (mapconcat (lambda (choice)
                                    (concat (propertize (car choice) 'face 'font-lock-type-face)
                                            ": "
                                            (cdr choice)))
                                  ", ")))))))
      (let ((result (assoc key choices)))
        (when result
          (let ((choice (cdr result)))
              (let ((start (region-beginning))
                    (end (region-end)))
                (goto-char end)
                (insert "#+END_" choice "\n")
                (goto-char start)
                (insert "#+BEGIN_" choice "\n")))
              (insert "#+BEGIN_" choice "\n")
              (save-excursion (insert "#+END_" choice))))))))))

;;bind to key
(define-key org-mode-map (kbd "C-<") 'org-begin-template)

Org-mode basics IV: formatting text and source code

This is a continuation of my series of introductory posts on org-mode that are focussed on simple text-based notes. We looked already at structuring your notes and adding tables and links and images to your notes. In this post we will look at formatting the text in your notes.

For today we will look at the effect of formatting on the plain text view of your notes, but very soon we will come to exporting your notes to html and pdf and we’ll see that the formatting is applied to the exported documents very nicely.

We will also look at including executable code in your notes. Hopefully you can see that by putting all these pieces together you can build a very powerful document of your research containing your data, notes, relevant links, source code and results.

As before I suggest adding the notes below to your growing org file. For technical reasons I have to display the notes as plain text below but if you paste them into your org file you’ll see them nicely formatted and coloured like this:

* Formatting text
** Simple formatting
You can apply simple formatting to your text by enclosing words in
special characters. These include
 - /italicised text/
 - *bold text*
 - _underlines_
 - =literal text=
 - ~code~ (generally appears the same as literal text)

Here are the full notes:

* Formatting text
** Simple formatting
You can apply simple formatting to your text by enclosing words in
special characters. These include
 - /italicised text/
 - *bold text*
 - _underlines_
 - =literal text=
 - ~code~ (generally appears the same as literal text)

** Formatted blocks of text
For longer pieces of text you can enclose the text in blocks marking
it as a specific sort of text. I commonly use these ones

This is an example block into which you can type text that you don't want org to mess with like a [[link]]. This will typically be rendered in a monospace font when exported.

This block encloses text that you want to appear as a quotation.

This text will be centred when it is exported.

You can save time typing out the block wrapper by using shortcuts. Go
to the start of a new line and type <e and press TAB and it will
expand to an example block. The same works for <q for quote and <c for

** LaTeX
Org-mode does a good job of understanding snippets of LaTeX (a
[[][powerful typesetting language]] used in scientific and other technical
documents). For example, it will correctly export simple superscripts
x^2 or subscripts x_0 or symbols like \alpha, \beta, \gamma.

Org also understands more complex LaTeX like this

x^2 + \left(\frac{y}{z}\right)^4 = 0

but for longer bits of LaTeX it is better to use a LaTeX block. You
start one with <l and TAB

LaTeX code goes here

** Source code blocks
It is also handy to include source code in your notes - on a new line
type <s and TAB to create a source block. You can tell org what type
of code is contained - in this case we'll put in some simple shell
code, so well put "sh" at the top of the block.

  echo "Hello $USER! Today is `date`"

You can get org to syntax highlight the text in the block by adding
the following to your [[][emacs config file]] (without the source block
wrapper of course).

#+BEGIN_SRC elisp
;;syntax highlight code blocks
(setq org-src-fontify-natively t)

What is more, when the cursor is inside a SRC block, you can use C-c '
to create a new temporary buffer in the major mode of the programming
language you have specified. Type some code in, and then type C-c '
again to come back to this buffer.

** Executing source code blocks
Org-mode can execute your source code blocks and add the output to
your file. This part of org-mode is called babel. I'll write more
about this later, but it is too cool not to mention here.

For example, take the simple code block we had above:

  echo "Hello $USER! Today is `date`"

Put the cursor inside the block and hit C-c C-c to execute it. You
will be asked to confirm and then you should see the output appear
like this:

Hello bjm! Today is Fri 25 Sep 2015 15:03:12 BST

You can do much more with this, like reading input data from a table
in the same file, creating images that appear in the file, extracting
(tangling) all the code snippets into one or more files to be executed
separately, and much more. [[][Here are some nice examples]].

You can tell org-mode which programming languages to support by adding
something like the following to your [[][emacs config file]]:

#+BEGIN_SRC elisp
;; Some initial languages we want org-babel to support
   (sh . t)
   (python . t)
   (R . t)
   (ditaa . t)
   (perl . t)
   (gnuplot t)

Org-mode basics III: add links and images to your notes

In this series of introductory posts on org-mode we have been focussing on simple text-based notes. We looked at structuring your notes and adding tables to your notes. Next we will look at adding links and images. Links can be to files, URLs or locations in the current org document. If the link is to an image then emacs can display it inline in the org document. This is handy for enhancing your notes and will also be useful when we come to look at exporting to different formats.

As before I suggest adding the notes below to your growing org file. Note that below I have formatted the notes as plain text because org-mode changes the appearance of links, such as hiding the [] around links, which is nice in your org-mode document but makes it harder for me to show you what is going on!

* Links and images
Org mode supports links to files, URLs, and to other points in the org
file. In this example let's use an image from my website. First copy
it to the current directory. You can do this within emacs but for now
just run this command in your terminal.

curl -o superman_cluster.gif

To add a link to a file use C-u C-c C-l and type the name of a file.
Use tab-completion to select the image we just copied and you will
then be asked for a description - you can press enter to leave this
blank. This will create a link that looks like this


If you do this in your org file, you wont see the [[ ]] above, instead
you'll see the text as a clickable link.

Since the file we have linked to is an image, we can tell emacs to the
image in the document using C-c C-x C-v and use the same command to
turn the image off again.

You can also click the link with the mouse, or use C-c C-o to follow
it, which might open your web browser, an image viewer or open a file
in emacs depending on the target of the link.

The structure of a link in org mode looks like this

[[link address][description]]

(I've enclosed the link in an example block which prevents org-mode
from trying to interpret as a real link, for the purpose of showing
its structure - we'll come back to blocks like this later.)

The link address is the URL or file name, and the description is the
text that is displayed, so we can replace our superman link with
something tidier like [[file:superman_cluster.gif][this]].

Links to web pages are easy - just put the http address in as the link
address. Use C-c C-l as a quick way to add such a link (remember we
used C-u C-c C-l is for adding a link to a file).

Links to other parts of the org file are added easily like [[Links and
images][this link]]. Because the address part of the link matches a
headline in this document, then org-mode points the link to that part
of the file. Clicking it will move the cursor there.

Finally, we can add a caption and a name to our image like this

#+CAPTION: Superman and a galaxy cluster
#+NAME: fig.super

which means we can refer to our image later with a link like this one

That’s all for now. Next time I think we’ll look at some simple text formatting.

Org-mode basics II: use simple tables in your notes

In the first post of this series we looked at using org-mode to structure your notes. Today we’ll look at adding simple tables to your notes. Later we’ll see how these tables can be used for advanced features like spreadsheet style calculations, or using them as the input and/or output of code, and also how they export nicely in html or pdf documents. For now we’ll just use them as simple static tables in our notes.

I suggest adding the text below to the org file from last time to build an org-mode notebook on how to keep org-mode notebooks!

* Tables
Hopefully you can see straight away that the simple structure provided
by org-mode gives a nice way to keep an electronic note book.

Often it is nice to include tables in our notes. Org handles this by
using | to separate columns, and a line of --- (inserted with C-c -)
to add horizontal lines.

Exercise: start typing in this table below; type the first line in
 1) when you get to the "s" of comments, press TAB to go to the next
 2) go up to the previous line and use C-c - to add the row of dashes
 3) next enter a few lines of data, using TAB to go through the
    cells - you should notice the columns changing width as needed

| ID | x |  y | comments       |
| A  | 2 |  4 | blah           |
| B  | 3 |  9 | blah           |
| C  | 4 | 16 | blah blah blah |
| D  | 5 | 25 | blah           |

Now, you can move rows and columns around using M-arrow and insert or
delete rows and columns using M-S-arrow. Try this out now.

** Creating and exporting tables
You can create an empty table using C-c | to run the command
org-table-create-or-convert-from-region, which will prompt for table
dimensions if no region is selected.

The same command can easily convert some text to a table; select the
following text and use C-c | to run the command
org-table-create-or-convert-from-region again to convert the text to a

 ID  x   y
 A   2   4
 B   3   9
 C   4  16
 D   5  25

You can also save tables to their own files by putting the cursor in
the table and using M-x org-table-export. You'll be asked for a
file name and a format. For the format, type orgtbl-to and press TAB
to see the available options (e.g. orgtbl-to-csv will convert to csv
in the output file).

** Formulae
You can use formulae to do arithmetic on tables, and use them like a
spreadsheet. This is something I keep meaning to use more often, but
don't generally find I need it. One useful command is C-c + which runs
org-table-sum to sum the numbers in the current column.

For more on this, see e.g. this introduction. Notice that we just
added a link in our org-mode file - this is a teaser for what we will
cover next!

Org-mode basics: structuring your notes

I’ve been putting off writing about org-mode as it is hard to know where to start. What is org-mode? From the org-mode web page

Org mode is for keeping notes, maintaining TODO lists, planning projects, and authoring documents with a fast and effective plain-text system.

For many people, org-mode is the reason they started using emacs. I only came to it after using emacs for about 10 years, but it was responsible for me moving from using emacs as a simple text editor to using emacs almost everywhere, and seeing a huge productivity boost.

Org-mode is very versatile, and I use it to (among other things):

  • Write general notes;
  • Write pdf lecture handouts and slides;
  • Write my research notes, analysis code, results, and final published papers in a single document allowing for reproducible research;
  • Manage my to do list and deadlines;
  • Write this blog;
  • Create static web pages;
  • Compose emails

One key thing to note is that while doing all these things, all org-mode documents are simple plain text that can be read in any text editor.

There are lots of org-mode tutorials out there such as

I don’t want to reinvent the wheel here, but I think the sheer amount that org-mode can do can be overwhelming for new users. So, in the spirit of this blog, I’ll write a series of posts to pick out some of the key features of org-mode that I use the most.

I’ll start here with the use of org-mode to make simple structured notes. This was the thing that got me hooked on org-mode, and everything else followed from here.

Org-mode is included in emacs, but you should install the most recent version (8.3.1 as of today).

The easiest way to get started is to open a new file in emacs with .org as the extension. Below is an example org document, and I would suggest typing this into your org-mode file to get a feeling for how the structure works.

* org-mode structure
Text in org is structured by headings, denoted by lines starting with
one or more * so we are currently in a section!

** A subheading
Starts with an extra * and so on

** navigation
Headings can be expanded or collapsed by moving to the (sub)heading
and pressing TAB. S-TAB cycles all headings. You can jump to next and
previous headings with C-c C-n and C-c C-p respectively.

You can move headings up and down to reorder them with the arrow keys,
using M-up or M-down. You can change the level of headings with M-left
and M-right (and use M-S-left and M-S-right to also change the levels
of and subheadings).

** lists
*** bullet lists
 - bullet lists can be created like this (start a line with one or
   more space and a -
 - pressing M-RET gives you a new bullet
 - we might also like nested bullets
   - like this one (I pressed M-RET TAB to indent it)
   - and another (M-RET now indents to the new level)
 - the nice thing is that for long lines of text, emacs wraps them
   so that they line up with the bullet
 - you can also reorder list items and change indentation using
   M-up or M-down just like with section headings
 - you can change bullet style using S-left and S-right

*** numbered lists
 1) numbered lists are also possible
 2) M-RET gives me a new number

*** checklists [/]
 - [ ] we can even have check lists
 - [ ] M-S-RET gives a new item with a check box
 - [ ] C-c C-c check/unchecks a box
 - [ ] you can have sub items
   + [ ] like this
   + [ ] that can be checked off individually
 - [ ] and you can track the number of items by adding [/] to the end
   of a line above a checklist - this updates when you check items off

*** definition lists
 - definition lists :: these are useful sometimes
 - item 2 :: M-RET again gives another item, and long lines wrap in a
      tidy way underneath the definition

I would suggest a couple of customisation to org-mode at this stage. Add the following to your emacs config file:

;; set maximum indentation for description lists
(setq org-list-description-max-indent 5)

;; prevent demoting heading also shifting text inside sections
(setq org-adapt-indentation nil)

That’s all for now. Try using org-mode to make simple notes and I think you’ll like the structure it gives you compared to simple text.