Skip to content

Commit 181fd3d

Browse files
Add Org tutorial
1 parent 0546071 commit 181fd3d

File tree

3 files changed

+306
-1
lines changed

3 files changed

+306
-1
lines changed
43.3 KB
Loading
47.4 KB
Loading

content/emacs/tutorial.md

Lines changed: 306 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ allowing you to read news, send emails, play media, and much more!
2424
> This [file][my-emacs-tut] is licensed under a free/libre copyleft license (GPL or CC BY-SA).
2525
>
2626
> **Changelog**
27+
> - 2024-10-07: added the *Org Mode* section.
2728
> - 2024-10-07: explained the *Compile* workflow.
2829
> - 2024-10-05: added the *Development* section.
2930
@@ -1136,7 +1137,309 @@ Emacs contains hundreds of microscopic quality of life features:
11361137

11371138
### ispell
11381139

1139-
### org mode
1140+
### Org Mode
1141+
1142+
[org-mode][org-mode] is arguably the most advanced system for taking notes and organizing information.
1143+
It is a free software that comes with Emacs.
1144+
1145+
[org-mode]: https://orgmode.org/features.html
1146+
1147+
In this section we will set up a simple, yet opinionated, Getting Things Done (GTD) workflow,
1148+
as presented by Nicolas P. Rougier in his [Emacs GTD][emacs-gtd].
1149+
This configuration has been proven to work, and I recommend using it as a starting point.
1150+
Add the following to your `~/.emacs`:
1151+
1152+
```scheme
1153+
;; Adjust org-mode for GTD
1154+
(use-package org
1155+
:config
1156+
;; Define task sequence, `|` separates the completed statuses.
1157+
(setq org-todo-keywords
1158+
'((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d)" "CANCELLED(c@/!)")))
1159+
1160+
;; Record the task completion date.
1161+
(setq org-log-done 'time))
1162+
```
1163+
1164+
#### Modern Style
1165+
1166+
Install a sleek-looking style with org-modern.
1167+
The following configuration defines a custom color for the `NEXT`
1168+
keyword that is used by GTD:
1169+
1170+
```scheme
1171+
;; Modern Org Style
1172+
(use-package org-modern
1173+
:ensure t
1174+
:config
1175+
;; Add styles for NEXT status
1176+
(setq org-modern-todo-faces
1177+
'(("NEXT" :foreground "purple" :weight bold :background "orange")))
1178+
;; Activate org-modern
1179+
(global-org-modern-mode))
1180+
```
1181+
1182+
#### Your First File
1183+
1184+
In this section, we will setup your first org file.
1185+
Create a file named `~/org/projects.org` with the following content:
1186+
1187+
```bash
1188+
* Project name
1189+
** A task
1190+
** Another task
1191+
1192+
* Another project
1193+
** Yet another task
1194+
```
1195+
1196+
Here are the commands you can apply to the headings:
1197+
1198+
| *Command* | *Description* |
1199+
|----------------------|------------------------------------|
1200+
| org-set-property | Assign a property like *CATEGORY*. |
1201+
| org-set-tags-command | Set the tags. |
1202+
| org-todo | Change the TODO state of an item. |
1203+
1204+
1205+
Perform the following actions:
1206+
1207+
- Set the **CATEGORY** property for the top headings with the project codename.
1208+
- Optionally add tags, such as related technology names.
1209+
- Set some task statuses with `C-c C-t` or `M-x org-todo`.
1210+
1211+
Your buffer should look like this:
1212+
1213+
![emacs-tut-org-file](media/emacs-tut-org-file.png)
1214+
1215+
> [!note]
1216+
> When a heading has no content, the ▶ indicator might get stuck in the open state: ▼.
1217+
> If that bothers you, add an empty paragraph with a dash `-`; then pressing `TAB` will work as expected.
1218+
1219+
1220+
#### Refile and Capture
1221+
1222+
To move tasks, use the `org-refile` command (`C-x C-w`).
1223+
Add the following settings to make your `projects.org` the default target.
1224+
1225+
```scheme
1226+
;; Allow moving task from anywhere into your projects:
1227+
(setq org-refile-targets '(("~/org/projects.org" :maxlevel . 1)))
1228+
1229+
;; Automatically save org files after refile
1230+
(advice-add 'org-refile :after (lambda (&rest _) (org-save-all-org-buffers)))
1231+
```
1232+
1233+
To add tasks, use the `org-capture` command.
1234+
Add the following settings:
1235+
1236+
```scheme
1237+
;; Setup capture template to write new tasks to ~/org/inbox.org
1238+
(setq org-capture-templates
1239+
'(("t" "todo" entry (file "~/org/inbox.org")
1240+
"* TODO %?\n/Entered on/ %U\n")))
1241+
1242+
;; Press F6 to capture a task
1243+
(global-set-key (kbd "<f6>") 'org-capture)
1244+
```
1245+
1246+
> [!tip]
1247+
> Capture tasks at any time by pressing <kbd>F6</kbd> <kbd>t</kbd>.
1248+
> Avoid using refile right away, use org-capture to quikcly move tasks off your mind.
1249+
1250+
1251+
#### Editing Org File
1252+
1253+
Org leverages a few keys to help with editing an Org file:
1254+
1255+
| *Key* | *Description* |
1256+
|---------------------------------|-----------------------------------|
1257+
| <kbd>TAB</kbd> | Cycle folding on current heading. |
1258+
| <kbd>shift</kbd>+<kbd>TAB</kbd> | Cycle folding on the whole file. |
1259+
| <kbd>alt</kbd>+<kbd>↑</kbd> | Move subtree up. |
1260+
| <kbd>alt</kbd>+<kbd>↓</kbd> | Move subtree down. |
1261+
| `M-g i` | Jump to a place in the buffer. |
1262+
| `M-RET` | Insert a new heading. |
1263+
1264+
1265+
#### Agenda
1266+
1267+
In this section, we will set up the agenda view, which is the main user interface you
1268+
will use to get things done.
1269+
Add the following to your Emacs config:
1270+
1271+
```scheme
1272+
;; org-agenda provides the GTD dashboard
1273+
(use-package org-agenda
1274+
:config
1275+
;; The agenda pulls data from the following files:
1276+
(setq org-agenda-files '("~/org/projects.org" "~/org/inbox.org"))
1277+
1278+
;; The GTD view
1279+
(setq-default org-agenda-custom-commands
1280+
'(("g" "Get Things Done (GTD)"
1281+
((agenda ""
1282+
((org-agenda-skip-function '(org-agenda-skip-entry-if 'deadline))
1283+
(org-deadline-warning-days 0)))
1284+
(todo "NEXT"
1285+
((org-agenda-skip-function '(org-agenda-skip-entry-if 'deadline))
1286+
(org-agenda-prefix-format " %i %-12:c [%e] ")
1287+
(org-agenda-overriding-header "\nTasks\n")))
1288+
(tags-todo "inbox"
1289+
((org-agenda-prefix-format " %?-12t% s")
1290+
(org-agenda-overriding-header "\nInbox\n")))
1291+
(tags "CLOSED>=\"<today>\""
1292+
((org-agenda-overriding-header "\nCompleted today\n")))))))
1293+
1294+
;; Press F4 to get things done!
1295+
(global-set-key (kbd "<f4>") (lambda () (interactive) (org-agenda nil "g"))))
1296+
```
1297+
1298+
> [!note]
1299+
> We don't need to use `use-package` because org-agenda is built into Emacs, but
1300+
> that helps keep your config nice and tidy.
1301+
1302+
This setup is well documented in Nicolas's [Emacs GTD][emacs-gtd] paper,
1303+
so I won't explain the settings in detail. What you need to know is that,
1304+
pressing <kbd>F4</kbd> will display a dashboard with four sections:
1305+
1306+
- The current week; see the next section for scheduling events.
1307+
- The list of tasks marked as `NEXT`.
1308+
- The list of tasks captured in the *inbox* that need to be refiled.
1309+
- The list of tasks completed today.
1310+
1311+
From that view, you can use the usual commands such as `org-todo` and `org-refile`.
1312+
Press `RET` to visit the file.
1313+
The view looks like this:
1314+
1315+
![emacs-tut-org-agenda](media/emacs-tut-org-agenda.png)
1316+
1317+
> [!note]
1318+
> Add the following header at the top of your `~/org/inbox.org` to see the untriaged tasks:
1319+
> ```bash
1320+
> #+FILETAGS: inbox
1321+
> ```
1322+
1323+
> [!tip]
1324+
> org-agenda is your main dashboard, it's not just an agenda.
1325+
1326+
[emacs-gtd]: https://www.labri.fr/perso/nrougier/GTD/index.html
1327+
1328+
#### Scheduling
1329+
1330+
Schedule a task with `org-schedule` (`C-x C-s`): this command pops up the Emacs calendar to
1331+
select a date. You can pick the date in the calendar by pressing `RET` on a day,
1332+
or you can directly type a date delta like:
1333+
1334+
- `+3h`: three hours from now.
1335+
- `+1d`: tomorrow.
1336+
- `+1w`: next week.
1337+
- `+1m`: next month.
1338+
- `Mon`: next Monday.
1339+
- `12-25`: next Dec 25th.
1340+
- `YY-MM-DD`: a fixed date.
1341+
1342+
When a task has a scheduled property, it will appear in the calendar.
1343+
If an open task is scheduled in the past, it appears as `Sched AGEx` in the current day,
1344+
reminding you that you forgot to complete a scheduled task.
1345+
1346+
You can make an event repeat with the following time syntax:
1347+
1348+
- `<2024-11-01 Fri +1m -3d>` for paying the rent; the task is required every month.
1349+
- `<2024-11-16 Sat .+2m>` for checking batteries; marking this DONE shifts the date to two months after today.
1350+
- `<2008-02-10 Sun ++1w>` for calling your mom; marking this DONE shifts the date by at least one week
1351+
1352+
For example, add the following headings to get started. Note that the first item is not a task, it's just a reminder for tri-weekly event:
1353+
1354+
```bash
1355+
* Work
1356+
** Sprint review
1357+
SCHEDULED: <2024-10-09 Wed 09:00 ++3w>
1358+
1359+
* Home
1360+
** TODO Pay rent
1361+
SCHEDULED: <2024-11-01 Fri +1m -3d>
1362+
1363+
** TODO Change toothbrush
1364+
SCHEDULED: <2024-11-16 Sat .+2m>
1365+
1366+
** TODO Call mum
1367+
SCHEDULED: <2008-02-10 Sun ++1w>
1368+
```
1369+
1370+
> [!tip]
1371+
> Schedule tasks with `C-c C-s` to put them at the top of your dashboard.
1372+
1373+
1374+
Finally, add the following to record anniversary events:
1375+
1376+
```bash
1377+
* Anniversaries
1378+
%%(diary-anniversary 3 20 1985) GNU Emacs was first released %d years ago
1379+
%%(diary-anniversary 5 29 2012) Zuul was created %d years ago
1380+
```
1381+
1382+
#### Journaling
1383+
1384+
You can write a journal with org-capture, add the following template:
1385+
1386+
```scheme
1387+
;; Add a new org-capture 'j' for journaling
1388+
(add-to-list 'org-capture-templates
1389+
'("j" "Journal" entry (file+olp+datetree "~/org/journal.org")
1390+
"* %?\n")
1391+
t)
1392+
```
1393+
1394+
Press <kbd>F6</kbd> <kbd>j</kbd> to record an event. It will be written to
1395+
your `~/org/journal.org` using date tree headings.
1396+
1397+
> [!tip]
1398+
> Use journal capture to keep track of what you are doing.
1399+
> That's a good practice for daily standups and monthly reviews.
1400+
1401+
#### Archive
1402+
1403+
When a task or project is no longer relevant, use the `org-archive-subtree`
1404+
to clean up your org file. This command moves the content to an
1405+
archive file that you can use to research the past events.
1406+
1407+
Here is a useful command to archive all completed tasks:
1408+
1409+
```scheme
1410+
;; From: https://stackoverflow.com/a/70131908
1411+
;; With auto save disabled
1412+
(defun org-archive-done-tasks ()
1413+
"Archive all tasks marked DONE in the file."
1414+
(interactive)
1415+
;; Disable auto save to avoid repeated file write.
1416+
(setq org-archive-subtree-save-file-p nil)
1417+
;; unwind-protext is like try/finally
1418+
(unwind-protect
1419+
;; process the entry in reverse to avoid changes in positioning
1420+
(mapc (lambda(entry)
1421+
(goto-char entry)
1422+
(org-archive-subtree))
1423+
(reverse (org-map-entries (lambda () (point)) "TODO=\"DONE\"" 'file)))
1424+
;; Enable auto save, even if an exception is raised.
1425+
(setq org-archive-subtree-save-file-p t))
1426+
(org-save-all-org-buffers))
1427+
```
1428+
1429+
---
1430+
To summarize, here are the main actions to implement GTD with Emacs:
1431+
- Add tasks with `org-capture`.
1432+
- Sort your inbox with `org-refile`.
1433+
- Groom your backlog with `org-todo`
1434+
- Plan your projects with `org-schedule`.
1435+
- Get things done with `org-agenda`.
1436+
1437+
Note that Org Mode also provides many additional features, such as:
1438+
- Time management with `org-clock`.
1439+
- Document publication with `org-export`.
1440+
- Literate source code editing with `org-babel`, which is similar to Jupyter notebooks.
1441+
1442+
However, I am not familiar with these features because I only use basic Org for Getting Things Done.
11401443

11411444
### notmuch
11421445

@@ -1152,6 +1455,8 @@ Emacs contains hundreds of microscopic quality of life features:
11521455

11531456
### vterm
11541457

1458+
### org-ql
1459+
11551460
### Tramp
11561461

11571462
```scheme

0 commit comments

Comments
 (0)