<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/atom.xsl" type="text/xsl"?><feed
	xmlns="http://www.w3.org/2005/Atom"
	xmlns:thr="http://purl.org/syndication/thread/1.0"
	xml:lang="en-US"
	><title>Sacha Chua - tag - wickedcool</title>
	<subtitle>Emacs, sketches, and life</subtitle>
	<link rel="self" type="application/atom+xml" href="https://sachachua.com/blog/tag/wickedcool/feed/atom/index.xml" />
  <link rel="alternate" type="text/html" href="https://sachachua.com/blog/tag/wickedcool" />
  <id>https://sachachua.com/blog/tag/wickedcool/feed/atom/index.xml</id>
  <generator uri="https://11ty.dev">11ty</generator>
	<updated>2008-01-05T03:57:00Z</updated>
<entry>
		<title type="html">Tagging in Org &#8211; plus bonus code for timeclocks and tags!</title>
		<link rel="alternate" type="text/html" href="https://sachachua.com/blog/2008/01/tagging-in-org-plus-bonus-code-for-timeclocks-and-tags/"/>
		<author><name><![CDATA[Sacha Chua]]></name></author>
		<updated>2008-01-05T09:14:14Z</updated>
    <published>2008-01-05T03:57:00Z</published>
    <category term="emacs" />
<category term="org" />
		<id>https://sachachua.com/blog/?p=4653</id>
		<content type="html"><![CDATA[<p>The section on projects introduced tags as a way to differentiate<br>
active and inactive projects. In this section, you&#8217;ll learn more about<br>
tags and how you can use them to filter your task list.</p>
<p>What&#8217;s a tag, anyway? In Org, tags are keywords at the end of<br>
headlines. Each tag can contain letters, numbers, and the symbols &#8216;_&#8217;<br>
and &#8216;@&#8217;. Tags begin and end with colons, and a single colon separates<br>
multiple tags. For example, you could have headlines like this:</p>
<pre class="example">
 * 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:</pre>
<p>One way to use tags is to filter your task list by priority. For<br>
example, you may want to focus on your urgent tasks first without<br>
getting distracted by other items on your task list. Another way to<br>
use tags is to keep track of the context of your tasks as suggested in<br>
GTD. By doing similar tasks together, you might be able to work more<br>
efficiently. For example, if you&#8217;re on the phone at the office, it may<br>
be a good idea to do all of your work-related phone calls. If you&#8217;re<br>
going to go to the post office, you might want to drop by the<br>
supermarket on your way back. You can use tags to categorize your<br>
headlines any way you want.</p>
<p>Tags can take advantage of the outline structure. For example,<br>
although the tasks &#8220;Buy milk&#8221;, &#8220;Call Mom&#8221;, and &#8220;Send letters&#8221; have one<br>
tag each, they also inherit the &#8220;PERSONAL&#8221; tag from the parent<br>
headline. A tag search for &#8220;PERSONAL&#8221; would display all three<br>
tasks. To customize this behavior, look at the documentation for the<br>
variables org-use-tag-inheritance and org-tags-match-list-sublevels.</p>
<p>Tags can help you organize and filter your task list. In this section,<br>
you&#8217;ll learn how to add tags to your headlines, view tagged items in<br>
your Org file and in your agenda, and define custom agenda<br>
views. You&#8217;ll also learn about custom tag searches and other<br>
interesting things you can do once you&#8217;ve tagged your headlines.</p>
<h4>Adding tags</h4>
<p class="first">You can edit your ~/organizer.org file and add tags manually by typing<br>
in :tagname: at the end of the headline. You can also add tags by<br>
typing C-c C-c (org-ctrl-c-ctrl-c) when the point is on a<br>
headline. Use M-TAB (complete-symbol) to complete a tag based on all<br>
the tags used in the current file. If Alt-TAB is not processed by<br>
Emacs, you can use ESC-TAB instead.</p>
<p>Separate multiple tags with a single colon, like this:<br>
(:@PHONE:URGENT:). The beginning and ending colons are optional when<br>
using C-c C-c (org-ctrl-c-ctrl-c), because the function will<br>
automatically add them.</p>
<p>If you add certain tags frequently, you can set up single-key<br>
shortcuts. For example, if you frequently tag tasks as &#8220;URGENT&#8221;, you<br>
may want to define a shortcut (at least until your life gets under<br>
control). You can assign shortcuts globally by adding this code to<br>
your ~/.emacs and evaluating it:</p>
<pre class="example">
   (setq org-tag-alist '(("URGENT" . ?u)
                         ("@PHONE" . ?p)
                         ("@ERRANDS" . ?e)))</pre>
<p>You can also set this on a per-file basis by adding the following line<br>
to the beginning of your file:</p>
<pre class="example">
 #+TAGS: URGENT(u) @PHONE(p) @ERRANDS(e)</pre>
<p>You can then use these C-c C-c (org-ctrl-c-ctrl-c) to enter these<br>
single-key shortcuts, ending it with RET. If you are assigning a<br>
single tag, type C-c C-c C-c (org-ctrl-c-ctrl-c, next change exits) to<br>
make it even faster by skipping the RET.</p>
<p>If you use single-key shortcuts, you&#8217;ll need another way to enter tags<br>
that start with the shortcut key. You can type them in manually, or<br>
you can use C-c C-c (org-ctrl-c-ctrl-c) and type TAB to enter in any tag.</p>
<p>To remove a tag, you could use C-c C-c (org-ctrl-c-ctrl-c) again, or<br>
delete it manually. To remove all tags, use C-c C-c<br>
(org-ctrl-c-ctrl-c) and press SPC.</p>
<h4>Viewing tagged items</h4>
<p></p>
<p class="first">Whether you want to view tagged headlines by themselves or in the<br>
context of your other headlines, tasks, and notes, Org has some nifty<br>
tagging features for you.</p>
<h5>Agenda view</h5>
<p>To view tagged headlines by themselves, use C-c a m (org-agenda,<br>
org-tags-view) and specify the search tag. For example, you can view<br>
your urgent tasks by specifying &#8220;URGENT&#8221;. Note that this command<br>
displays the top headlines matching that tag, whether they&#8217;re tasks or<br>
not. For example, if you searched for &#8220;WORK&#8221;, you would just get the<br>
&#8220;* Work&#8221; headline. To view tagged tasks, use C-c a M (org-agenda,<br>
org-tags-view with a prefix argument). This shows only the tasks that<br>
have that tag.</p>
<p>To search for a combination of tags, you can combine tags like this:</p>
<pre class="example">
WORK&amp;@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</pre>
<p>If you check certain lists often, you might want to create a custom<br>
agenda command for them. In the section on Projects, you configured<br>
custom agenda commands for active and inactive projects by adding the<br>
following code in your ~/.emacs:</p>
<pre class="example">
(setq org-agenda-custom-commands
      '(("p" tags "PROJECT-MAYBE-DONE" nil)
        ("m" tags "PROJECT&amp;MAYBE" nil)
        ("a" "My agenda"
         ((org-agenda-list)
          (tags "PROJECT-MAYBE-DONE")))
        ;; ... put your other custom commands here
       ))</pre>
<p>You can use the same idea to create quick custom views for your other<br>
tagged tasks. For example, to create custom views for your urgent work<br>
tasks and your phone calls, modify the org-agenda-custom-commands<br>
setting in your ~/.emacs to be like this:</p>
<pre class="example">
(setq org-agenda-custom-commands
      '(("u" todo "WORK&amp;URGENT" nil)               ;; (1)
        ("c" todo "WORK&amp;@PHONE" nil)               ;; (2)
        ("h" todo "PERSONAL-@ERRANDS" nil)         ;; (3)
        ("p" tags "PROJECT-MAYBE-DONE" nil)        ;; (4)
        ("m" tags "PROJECT&amp;MAYBE" nil)
        ("a" "My agenda"
         ((org-agenda-list)
          (tags-todo "URGENT")                     ;; (5)
          (tags "PROJECT-MAYBE-DONE")))            ;; (6)
        ;; ... put your other custom commands here
       ))</pre>
<ul>
<li>(1) &#8220;u&#8221; is for &#8220;urgent&#8221;, &#8220;todo&#8221; specifies that TODO headlines are to be shown, &#8220;WORK&amp;URGENT&#8221; is the query string, and the last item means that there aren&#8217;t any options</li>
<li>(2) &#8220;c&#8221; is for &#8220;call&#8221;</li>
<li>(3) &#8220;h&#8221; is for &#8220;home&#8221;</li>
<li>(4) The second item here is &#8220;tags&#8221; instead of &#8220;todo&#8221;, which means that the highest-level matching headlines should be shown whether or not they&#8217;re tasks.</li>
<li>(5) This is how to add a tag search for tasks into a custom agenda command.</li>
<li>(6) This is how to add a tag search for headlines into a custom agenda command.</li>
</ul>
<p>Sometimes you&#8217;ll want to see more context instead of just a list of<br>
headlines. You can jump from your Org agenda to the corresponding<br>
headline by pressing RET (org-agenda-switch-to) on the entry. You can<br>
also quickly browse through the headlines in another window by<br>
pressing f (org-agenda-follow-mode) while in the Org agenda view, then<br>
moving your point to the different lines. These commands work with the<br>
summary in the Org agenda view.</p>
<p>If you want to show only matching headlines in your ~/organizer.org<br>
file, you can use Org&#8217;s sparse tree search commands.</p>
<h5>In your agenda file</h5>
<p>A sparse tree shows only the matching headlines in the context of the<br>
headlines above them. This is useful when you want to see your tasks<br>
within your outline structure. All other headlines are collapsed so<br>
that they&#8217;re easy to skip. To do a sparse tree search, type C-c \<br>
(org-tags-sparse-tree). You can then expand and collapse subtrees with<br>
the TAB (org-cycle) command. To limit the search to only task<br>
headlines, type C-u C-c \ (org-tags-sparse-tree with a prefix).</p>
<h4>Other cool things you can do with tags</h4>
<p></p>
<p class="first">And if you ever want to know how much time you spent on urgent tasks,<br>
you can call the following function from your organizer.org file with:</p>
<pre class="example">
M-x wicked/org-calculate-tag-time RET URGENT RET</pre>
<p>to see something like this:</p>
<pre class="example">
Time: 98:44 (98 hours and 44 minutes)</pre>
<p>You can call it with a prefix in order to be prompted for a start time<br>
(inclusive) and end time (exclusive).</p>
<p>Here&#8217;s the code to add to your ~/.emacs:</p>
<pre class="example">
(defun wicked/org-calculate-tag-time (matcher &amp;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 " *\\(\\&lt;\\("
		     (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
		     (org-re
		      "\\&gt;\\)\\)? *\\(.*?\\)\\(:[[: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 (&gt;= 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))</pre>
<p>Now you can slice and dice your timeclock records any way you want, thanks to tags!</p>
<p>Random Emacs symbol: cc-imenu-java-generic-expression &#8211; Variable: Imenu generic expression for Java mode.  See `imenu-generic-expression&#8217;.</p>

<p>You can <a href="https://sachachua.com/blog/2008/01/tagging-in-org-plus-bonus-code-for-timeclocks-and-tags/#comment">view 14 comments</a> or <a href="mailto:sacha@sachachua.com?subject=Comment%20on%20https%3A%2F%2Fsachachua.com%2Fblog%2F2008%2F01%2Ftagging-in-org-plus-bonus-code-for-timeclocks-and-tags%2F&body=Name%20you%20want%20to%20be%20credited%20by%20(if%20any)%3A%20%0AMessage%3A%20%0ACan%20I%20share%20your%20comment%20so%20other%20people%20can%20learn%20from%20it%3F%20Yes%2FNo%0A">e-mail me at sacha@sachachua.com</a>.</p>]]></content>
		</entry>
</feed>