6089 comments
2357 subscribers
6230 on Twitter
Subscribe! Feed reader E-mail

Tagging in Org – plus bonus code for timeclocks and tags!

The section on projects introduced tags as a way to differentiate
active and inactive projects. In this section, you’ll learn more about
tags and how you can use them to filter your task list.

What’s a tag, anyway? In Org, tags are keywords at the end of
headlines. Each tag can contain letters, numbers, and the symbols ‘_’
and ‘@’. Tags begin and end with colons, and a single colon separates
multiple tags. For example, you could have headlines like this:

 * Personal                                     :PERSONAL:
 ** TODO Buy milk                               :@ERRANDS:
 ** TODO Call Mom                               :@PHONE:
 ** TODO Send letters                           :@ERRANDS:
 * Work                                         :WORK:
 ** TODO Call John about report                 :@PHONE:JOHN:
 ** TODO Prepare for presentation on Monday
 ** TODO Call Mary about the presentation       :@PHONE:URGENT:MARY:

One way to use tags is to filter your task list by priority. For
example, you may want to focus on your urgent tasks first without
getting distracted by other items on your task list. Another way to
use tags is to keep track of the context of your tasks as suggested in
GTD. By doing similar tasks together, you might be able to work more
efficiently. For example, if you’re on the phone at the office, it may
be a good idea to do all of your work-related phone calls. If you’re
going to go to the post office, you might want to drop by the
supermarket on your way back. You can use tags to categorize your
headlines any way you want.

Tags can take advantage of the outline structure. For example,
although the tasks “Buy milk”, “Call Mom”, and “Send letters” have one
tag each, they also inherit the “PERSONAL” tag from the parent
headline. A tag search for “PERSONAL” would display all three
tasks. To customize this behavior, look at the documentation for the
variables org-use-tag-inheritance and org-tags-match-list-sublevels.

Tags can help you organize and filter your task list. In this section,
you’ll learn how to add tags to your headlines, view tagged items in
your Org file and in your agenda, and define custom agenda
views. You’ll also learn about custom tag searches and other
interesting things you can do once you’ve tagged your headlines.

Adding tags

You can edit your ~/organizer.org file and add tags manually by typing
in :tagname: at the end of the headline. You can also add tags by
typing C-c C-c (org-ctrl-c-ctrl-c) when the point is on a
headline. Use M-TAB (complete-symbol) to complete a tag based on all
the tags used in the current file. If Alt-TAB is not processed by
Emacs, you can use ESC-TAB instead.

Separate multiple tags with a single colon, like this:
(:@PHONE:URGENT:). The beginning and ending colons are optional when
using C-c C-c (org-ctrl-c-ctrl-c), because the function will
automatically add them.

If you add certain tags frequently, you can set up single-key
shortcuts. For example, if you frequently tag tasks as “URGENT”, you
may want to define a shortcut (at least until your life gets under
control). You can assign shortcuts globally by adding this code to
your ~/.emacs and evaluating it:

   (setq org-tag-alist '(("URGENT" . ?u)
                         ("@PHONE" . ?p)
                         ("@ERRANDS" . ?e)))

You can also set this on a per-file basis by adding the following line
to the beginning of your file:

 #+TAGS: URGENT(u) @PHONE(p) @ERRANDS(e)

You can then use these C-c C-c (org-ctrl-c-ctrl-c) to enter these
single-key shortcuts, ending it with RET. If you are assigning a
single tag, type C-c C-c C-c (org-ctrl-c-ctrl-c, next change exits) to
make it even faster by skipping the RET.

If you use single-key shortcuts, you’ll need another way to enter tags
that start with the shortcut key. You can type them in manually, or
you can use C-c C-c (org-ctrl-c-ctrl-c) and type TAB to enter in any tag.

To remove a tag, you could use C-c C-c (org-ctrl-c-ctrl-c) again, or
delete it manually. To remove all tags, use C-c C-c
(org-ctrl-c-ctrl-c) and press SPC.

Viewing tagged items

Whether you want to view tagged headlines by themselves or in the
context of your other headlines, tasks, and notes, Org has some nifty
tagging features for you.

Agenda view

To view tagged headlines by themselves, use C-c a m (org-agenda,
org-tags-view) and specify the search tag. For example, you can view
your urgent tasks by specifying “URGENT”. Note that this command
displays the top headlines matching that tag, whether they’re tasks or
not. For example, if you searched for “WORK”, you would just get the
“* Work” headline. To view tagged tasks, use C-c a M (org-agenda,
org-tags-view with a prefix argument). This shows only the tasks that
have that tag.

To search for a combination of tags, you can combine tags like this:

WORK&@PHONE           only your work phone calls
PERSONAL-@ERRANDS     personal tasks, but without errands
JOHN|MARY             Anything tagged with "JOHN" or "MARY"
                      For example, if you're going to have a meeting with both of them

If you check certain lists often, you might want to create a custom
agenda command for them. In the section on Projects, you configured
custom agenda commands for active and inactive projects by adding the
following code in your ~/.emacs:

(setq org-agenda-custom-commands
      '(("p" tags "PROJECT-MAYBE-DONE" nil)
        ("m" tags "PROJECT&MAYBE" nil)
        ("a" "My agenda"
         ((org-agenda-list)
          (tags "PROJECT-MAYBE-DONE")))
        ;; ... put your other custom commands here
       ))

You can use the same idea to create quick custom views for your other
tagged tasks. For example, to create custom views for your urgent work
tasks and your phone calls, modify the org-agenda-custom-commands
setting in your ~/.emacs to be like this:

(setq org-agenda-custom-commands
      '(("u" todo "WORK&URGENT" nil)               ;; (1)
        ("c" todo "WORK&@PHONE" nil)               ;; (2)
        ("h" todo "PERSONAL-@ERRANDS" nil)         ;; (3)
        ("p" tags "PROJECT-MAYBE-DONE" nil)        ;; (4)
        ("m" tags "PROJECT&MAYBE" nil)
        ("a" "My agenda"
         ((org-agenda-list)
          (tags-todo "URGENT")                     ;; (5)
          (tags "PROJECT-MAYBE-DONE")))            ;; (6)
        ;; ... put your other custom commands here
       ))
  • (1) “u” is for “urgent”, “todo” specifies that TODO headlines are to be shown, “WORK&URGENT” is the query string, and the last item means that there aren’t any options
  • (2) “c” is for “call”
  • (3) “h” is for “home”
  • (4) The second item here is “tags” instead of “todo”, which means that the highest-level matching headlines should be shown whether or not they’re tasks.
  • (5) This is how to add a tag search for tasks into a custom agenda command.
  • (6) This is how to add a tag search for headlines into a custom agenda command.

Sometimes you’ll want to see more context instead of just a list of
headlines. You can jump from your Org agenda to the corresponding
headline by pressing RET (org-agenda-switch-to) on the entry. You can
also quickly browse through the headlines in another window by
pressing f (org-agenda-follow-mode) while in the Org agenda view, then
moving your point to the different lines. These commands work with the
summary in the Org agenda view.

If you want to show only matching headlines in your ~/organizer.org
file, you can use Org’s sparse tree search commands.

In your agenda file

A sparse tree shows only the matching headlines in the context of the
headlines above them. This is useful when you want to see your tasks
within your outline structure. All other headlines are collapsed so
that they’re easy to skip. To do a sparse tree search, type C-c \
(org-tags-sparse-tree). You can then expand and collapse subtrees with
the TAB (org-cycle) command. To limit the search to only task
headlines, type C-u C-c \ (org-tags-sparse-tree with a prefix).

Other cool things you can do with tags

And if you ever want to know how much time you spent on urgent tasks,
you can call the following function from your organizer.org file with:

M-x wicked/org-calculate-tag-time RET URGENT RET

to see something like this:

Time: 98:44 (98 hours and 44 minutes)

You can call it with a prefix in order to be prompted for a start time
(inclusive) and end time (exclusive).

Here’s the code to add to your ~/.emacs:

(defun wicked/org-calculate-tag-time (matcher &optional ts te)
  "Return the total minutes clocked in headlines matching MATCHER.
MATCHER is a string or a Lisp form to be evaluated, testing if a
given set of tags qualifies a headline for inclusion. TS and TE
are time start (inclusive) and time end (exclusive). Call with a
prefix to be prompted for TS and TE.

For example, to see how much time you spent on tasks tagged as
URGENT, call M-x wicked/org-calculate-tag-time RET URGENT RET. To
see how much time you spent on tasks tagged as URGENT today, call
C-u M-x wicked/org-calculate-tag-time RET URGENT RET . RET +1 RET."
  (interactive (list
		(read-string "Tag query: ")
		(if current-prefix-arg (org-read-date))
		(if current-prefix-arg (org-read-date))))
  ;; Convert strings to proper arguments
  (if (stringp matcher) (setq matcher (cdr (org-make-tags-matcher matcher))))
  (if (stringp ts)
      (setq ts (time-to-seconds (apply 'encode-time (org-parse-time-string ts)))))
  (if (stringp te)
      (setq te (time-to-seconds (apply 'encode-time (org-parse-time-string te)))))
  (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
		     (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
		     (org-re
		      "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
	 (case-fold-search nil)
         lspos
	 tags tags-list tags-alist (llast 0) rtn level category i txt p
	 marker entry priority (total 0))
    (save-excursion
      (org-clock-sum ts te)
      (goto-char (point-min))
      (while (re-search-forward re nil t)
	(catch :skip
	  (setq tags (if (match-end 4) (match-string 4)))
	  (goto-char (setq lspos (1+ (match-beginning 0))))
	  (setq level (org-reduced-level (funcall outline-level))
		category (org-get-category))
	  (setq i llast llast level)
	  ;; remove tag lists from same and sublevels
	  (while (>= i level)
	    (when (setq entry (assoc i tags-alist))
	      (setq tags-alist (delete entry tags-alist)))
	    (setq i (1- i)))
	  ;; add the nex tags
	  (when tags
	    (setq tags (mapcar 'downcase (org-split-string tags ":"))
		  tags-alist
		  (cons (cons level tags) tags-alist)))
	  ;; compile tags for current headline
	  (setq tags-list
		(if org-use-tag-inheritance
		    (apply 'append (mapcar 'cdr tags-alist))
		  tags))
	  (when (and (eval matcher)
		     (or (not org-agenda-skip-archived-trees)
			 (not (member org-archive-tag tags-list))))
	    ;; Get the time for the headline at point
	    (goto-char (line-beginning-position))
	    (setq total (+ total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)))
	    ;; if we are to skip sublevels, jump to end of subtree
	    (org-end-of-subtree t)))))
    (if (interactive-p)
	(let* ((h (/ total 60))
	       (m (- total (* 60 h))))
	  (message "Time: %d:%02d (%d hours and %d minutes)" h m h m)))
    total))

Now you can slice and dice your timeclock records any way you want, thanks to tags!

Random Emacs symbol: cc-imenu-java-generic-expression – Variable: Imenu generic expression for Java mode. See `imenu-generic-expression’.

On Technorati: , ,

Short URL: http://sachachua.com/blog/p/4653
  • harsha

    hi, i’ve just started using org and am loving it. just wanted to know, is there some way of lmiting inheritance to a spcific set of tags?
    harsha

  • Raymond Zeitler

    Hi! Guess who just started to learn org-mode? :)

    Someone suggested that I could implement a platform independent password manager using org-mode (with org-crypt). It’s not quite working yet, but I’m also having fun with many other features!

    Org is Amazing! It’s daunting, but luckily there are great tutorials, and the Customization is well-done.

  • http://sachachua.com Sacha Chua

    Aha! Welcome to the Org side, old friend. <laugh> John Wiegley’s on Org, too. It’s all good.

    That does sound promising!

On This Day...

  • 2013: Imagining the next five years and planning 2013 — One of the assignments in the Rockstar Scribe course I’m taking through Alphachimp University (affiliate link) is to sketch where [...]
  • 2012: Quantified Awesome: Squishing my excuses — I’ve been fiddling with Quantified Awesome, this personal dashboard that I’m building so that I can keep track of what’s [...]
  • 2011: Sketches: If you want to make the most of your next conference, you should blog
  • 2010: Weekly review: Weeks ending December 27, 2009 and January 3, 2010 — Busy, busy, busy! From the previous week’s plans for the week ending December 27, 2009: Work Set up Idea Lab needed for January [...]
  • 2010: How I explore my interests — Looking for your passions? You might be starting with the wrong question. Except in rare circumstances, passion doesn’t hit people [...]
  • 2009: Hooray! Tax-free savings account! — To encourage people to save, the Canadian government created a tax-free savings account. You put after-tax dollars into it, and [...]
  • 2009: Ideas for making my work more effective and efficient, creating value, and rocking my work — Change to Ubuntu Set up virtual machine for my Windows partition Use Emacs to handle my mail? Hard to do calendar acceptance Set [...]
  • 2008: Working two jobs — “Next time I do this, I’m going to have to find a day job that isn’t this similar to my [...]
  • 2007: Deja vu — The Internet is still crawling. Takes me back to the days of BBSing, when you could type faster than the modem [...]
  • 2007: Hate is as useful as love — … sometimes even more so. I’m beginning to hate the stress I feel about this thesis. The best way to deal with [...]
  • 2007: Working through the funk — I felt tired this morning. Things weren’t working smoothly. It turns out that the Philippines is probably not part of the [...]
  • 2007: Whatever gets the job done — I should’ve tried dialing into the IBM conference center last night to confirm that our landline could connect to it. Had [...]
  • 2007: It’s nice to be missed! — Bryce Johnson e-mailed me to ask me why I hadn’t signed up for Enterprise Camp. I sent my regrets and explained that [...]
  • 2006: It’s official – I’ll be working on social search! — I’m thrilled to report that a large company has given the go signal for research on social computing. Social search, in [...]
  • 2005: Emacs channel chat logs — Also, if someone can help me set up logging (split into text files by day, please) for irc://irc.freenode.net/#emacs, I volunteer to summarize [...]
  • 2005: Looking for Emacs blogs — I’d love to read more about the wonderful editor/way-of-life that is Emacs. Know of any Emacs-related blogs? (Occasional off-topics are fine.) Please [...]
  • 2005: debian-installer Tagalog translation complete! — ([[OpenSourceInThePhilippines#note4][OpenSourceInThePhilippines:4]]”[[oss]]”[[l10n]]”[[Localization]]) Eric Pareja and other volunteer translators have finished the translating level 1 of the debian-installer into Tagalog. Please check out the [...]
  • 2005: A question of scale — ([[OpenSource#note2][OpenSource:2]]”[[oss]]”[[OpenSourceIssues#note2][OpenSourceIssues:2]]”[[ToBlog#note2][ToBlog:2]]) Open source allows people to work on an individual basis. Developers can jumpstart their projects by using existing code, creating [...]
  • 2005: xtla goodness ([[EmacsTips#note19][EmacsTips:19]]”[[emacs]]) — I used xtla to browse my TLA archives today. xtla’s bookmarks and missing patch summary made merging missing patches much easier. [...]
  • 2005: Planner cited as a reason to defect to Emacs — (PlannerModeMaintenance”[[GoodKarma#note2][GoodKarma:2]]) You point is well taken. I find myself living in an application more and more. At work, it’s my [...]
  • 2005: Ethical issues in open source — ([[OpenSource#note1][OpenSource:1]]”[[oss]]”[[OpenSourceIssues#note1][OpenSourceIssues:1]]”[[ToBlog#note1][ToBlog:1]]) One of my students e-mailed me asking for help finding interesting ethical issues in open source. Here’s the partial list [...]
  • 2004: Cat collar — That hated bell! Although it provided music for her every move, it stopped her from being sneaky – and being sneaky [...]
  • 2004: Counting in Dutch — - een kus, twee kussen - ik kus, jij kust, hij kust, wij kussen, jullie kussen, zij kussen
  • 2004: Factoid surprise — Looks like my bot implants are working. Remember that hack I added to ../emacs/bbdb-config.el to let me hippie-expand factoids? Earlier in #linuxhelp, [...]

Get the highlights as a PDF!

Stories from my Twenties: Highlights from a Decade of Blogging

Free sample!