Emacs 系列（二）：Org 模式介绍
Emacs
![](/data/attachment/album/201812/04/222917qmi4mc9o34euemqr.png)


在我 Emacs 系列中的[第一篇文章](/article-10297-1.html)里，我介绍了我在用了几十年的 vim 后转向了 Emacs，Org 模式就是我为什么这样做的原因。


Org 模式的精简和高效真的震惊了我，它真的是个“杀手”应用。


### 所以，Org 模式到底是什么呢？


这是我昨天写的：



> 
> 它是一个组织信息的平台，它的主页上这样写着：“一切都是纯文本：Org 模式用于记笔记、维护待办事项列表、计划项目和使用快速有效的纯文本系统编写文档。”
> 
> 
> 


这是事实，但并不是很准确。Org 模式是一个你用来组织事务的小工具。它有一些非常合理的默认设置，但也允许你自己定制。


主要突出在这几件事上：


* **维护待办事项列表**：项目可以分散在 Org 文件中，包含附件，有标签、截止日期、时间表。有一个方便的“日程”视图，显示需要做什么。项目也可以重复。
* **编写文档**：Org 模式有个特殊的功能来生成 HTML、LaTeX、幻灯片（用 LaTeX beamer）和其他所有的格式。它也支持直接在缓冲区中运行和以 Emacs 所支持的的语言进行<ruby> 文学编程 <rt>  literate programming </rt></ruby>。如果你想要深入了解这项功能的话，参阅[这篇文学式 DevOps 的文章](http://www.howardism.org/Technical/Emacs/literate-devops.html)。而 [整个 Worg 网站](https://orgmode.org/worg/) 是用 Org 模式开发的。
* **记笔记**：对，它也能做笔记。通过全文搜索，文件的交叉引用（类似 wiki），UUID，甚至可以与其他的系统进行交互（通过 Message-ID 与 mu4e 交互，通过 ERC 的日志等等……）。


### 入门


我强烈建议去阅读 [Carsten Dominik 关于 Org 模式的一篇很棒的 Google 讲话](https://www.youtube.com/watch?v=oJTwQvgfgMM)。那篇文章真的很赞。


在 Emacs 中带有 Org 模式，但如果你想要个比较新的版本的话，Debian 用户可以使用命令 `apt-get install org-mode` 来更新，或者使用 Emacs 的包管理系统命令 `M-x package-install RET org-mode RET`。


现在，你可能需要阅读一下 Org 模式的精简版教程中的[导读部分](https://orgmode.org/guide/Introduction.html#Introduction)，特别注意，你要设置下[启动部分](https://orgmode.org/guide/Activation.html#Activation)中提到的那些键的绑定。


### 一份好的教程


我会给出一些好的教程和介绍的链接，但这篇文章不会是一篇教程。特别是在本文末尾，有两个很不错的视频链接。


### 我的一些配置


我将在这里记录下一些我的配置并介绍它的作用。你没有必要每行每句将它拷贝到你的配置中 —— 这只是一个参考，告诉你哪些可以配置，要怎么在手册中查找，或许只是一个“我现在该怎么做”的参考。


首先，我将 Emacs 的编码默认设置为 UTF-8。



```
(prefer-coding-system 'utf-8) 
(set-language-environment "UTF-8")
```

Org 模式中可以打开 URL。默认的，它会在 Firefox 中打开，但我喜欢用 Chromium。



```
(setq browse-url-browser-function 'browse-url-chromium)
```

我把基本的键的绑定和设为教程里的一样，再加上 `M-RET` 的操作的配置。



```
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cc" 'org-capture)
(global-set-key "\C-cb" 'org-iswitchb)

(setq org-M-RET-may-split-line nil)
```

### 捕获配置


我可以在 Emacs 的任何模式中按 `C-c c`，按下后它就会[帮我捕获某些事](https://orgmode.org/guide/Capture.html#Capture)，其中包括一个指向我正在处理事务的链接。


你可以通过定义[捕获模板](https://orgmode.org/guide/Capture-templates.html#Capture-templates)来配置它。我将保存两个日志文件，作为会议、电话等的通用记录。一个是私人用的，一个是办公用的。如果我按下 `C-c c j`,它就会帮我捕获为私人项. 下面包含 `%a` 的配置是表示我当前的位置（或是我使用 `C-c l` 保存的链接）的链接。



```
(setq org-default-notes-file "~/org/tasks.org")
(setq org-capture-templates
      '(
        ("t" "Todo" entry (file+headline "inbox.org" "Tasks")
         "* TODO %?\n  %i\n  %u\n  %a")
        ("n" "Note/Data" entry (file+headline "inbox.org" "Notes/Data")
         "* %?   \n  %i\n  %u\n  %a")
        ("j" "Journal" entry (file+datetree "~/org/journal.org")
         "* %?\nEntered on %U\n %i\n %a")
        ("J" "Work-Journal" entry (file+datetree "~/org/wjournal.org")
         "* %?\nEntered on %U\n %i\n %a")
        ))
(setq org-irc-link-to-logs t)
```

我喜欢通过 UUID 来建立链接，这让我在文件之间移动而不会破坏位置。当我要 Org 存储一个链接目标以便将来插入时，以下配置有助于生成 UUID。



```
(require 'org-id)
(setq org-id-link-to-org-use-id 'create-if-interactive)
```

### 议程配置


我喜欢将星期天作为一周的开始，当我将某件事标记为完成时，我也喜欢记下时间。



```
(setq org-log-done 'time)
(setq org-agenda-start-on-weekday 0)
```

### 文件归档配置


在这我将配置它，让它知道在议程中该使用哪些文件，而且在纯文本的搜索中添加一点点小功能。我喜欢保留一个通用的文件夹（我可以从其中移动或“重新归档”内容），然后将个人和工作项的任务、日志和知识库分开。



```
  (setq org-agenda-files (list "~/org/inbox.org"
                               "~/org/email.org"
                               "~/org/tasks.org"
                               "~/org/wtasks.org"
                               "~/org/journal.org"
                               "~/org/wjournal.org"
                               "~/org/kb.org"
                               "~/org/wkb.org"
  ))
  (setq org-agenda-text-search-extra-files
        (list "~/org/someday.org"
              "~/org/config.org"
  ))

  (setq org-refile-targets '((nil :maxlevel . 2)
                             (org-agenda-files :maxlevel . 2)
                             ("~/org/someday.org" :maxlevel . 2)
                             ("~/org/templates.org" :maxlevel . 2)
                             )
        )
(setq org-outline-path-complete-in-steps nil)         ; Refile in a single go
(setq org-refile-use-outline-path 'file)
```

### 外观配置


我喜欢一个较漂亮的的屏幕。在你开始习惯 Org 模式之后，你可以试试这个。



```
(add-hook 'org-mode-hook
          (lambda ()
            (org-bullets-mode t)))
(setq org-ellipsis "⤵")
```

### 下一篇


希望这篇文章展示了 Org 模式的一些功能。接下来，我将介绍如何定制 `TODO` 关键字和标记、归档旧任务、将电子邮件转发到 Org 模式，以及如何使用 `git` 在不同电脑之间进行同步。


你也可以查看[本系列的所有文章列表](https://changelog.complete.org/archives/tag/emacs2018)。




---


via: <https://changelog.complete.org/archives/9865-emacs-2-introducing-org-mode>


作者：[John Goerzen](http://changelog.complete.org/archives/author/jgoerzen) 选题：[lujun9972](https://github.com/lujun9972) 译者：[oneforalone](https://github.com/oneforalone) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In [my first post in my series on Emacs](https://changelog.complete.org/archives/9861-emacs-1-ditching-a-bunch-of-stuff-and-moving-to-emacs-and-org-mode), I described returning to Emacs after over a decade of vim, and org-mode being the reason why.

I really am astounded at the usefulness, and simplicity, of org-mode. It is really a killer app.

**So what exactly is org-mode?**

I wrote yesterday:

It’s an information organization platform. Its website says “Your life in plain text: Org mode is for keeping notes, maintaining TODO lists, planning projects, and authoring documents with a fast and effective plain-text system.”


That’s true, but doesn’t quite capture it. org-mode is a toolkit for you to organize things. It has reasonable out-of-the-box defaults, but it’s designed throughout for you to customize.

To highlight a few things:

**Maintaining TODO lists**: items can be scattered across org-mode files, contain attachments, have tags, deadlines, schedules. There is a convenient “agenda” view to show you what needs to be done. Items can repeat.**Authoring documents**: org-mode has special features for generating HTML, LaTeX, slides (with LaTeX beamer), and all sorts of other formats. It also supports direct evaluation of code in-buffer and literate programming in virtually any Emacs-supported language. If you want to bend your mind on this stuff, read[this article on literate devops](http://www.howardism.org/Technical/Emacs/literate-devops.html). The[entire Worg website](https://orgmode.org/worg/)

is made with org-mode.**Keeping notes**: yep, it can do that too. With full-text search, cross-referencing by file (as a wiki), by UUID, and even into other systems (into mu4e by Message-ID, into ERC logs, etc, etc.)

**Getting started**

I highly recommend watching [Carsten Dominik’s excellent Google Talk on org-mode](https://www.youtube.com/watch?v=oJTwQvgfgMM). It is an excellent introduction.

org-mode is included with Emacs, but you’ll often want a more recent version. Debian users can `apt-get install org-mode`, or it comes with the Emacs packaging system; `M-x package-install RET org-mode RET` may do it for you.

Now, you’ll probably want to start with the org-mode compact guide’s [introduction section](https://orgmode.org/guide/Introduction.html#Introduction), noting in particular to set the keybindings mentioned in the [activation section](https://orgmode.org/guide/Activation.html#Activation).

**A good tutorial…**

I’ve linked to a number of excellent tutorials and introductory items; this post is not going to serve as a tutorial. There are two good videos linked at the end of this post, in particular.

**Some of my configuration**

I’ll document some of my configuration here, and go into a bit of what it does. This isn’t necessarily because you’ll want to copy all of this verbatim — but just to give you a bit of an idea of some of what can be configured, an idea of what to look up in the manual, and maybe a reference for “now how do I do that?”

First, I set up Emacs to work in UTF-8 by default.


(prefer-coding-system 'utf-8)

(set-language-environment "UTF-8")


org-mode can follow URLs. By default, it opens in Firefox, but I use Chromium.


(setq browse-url-browser-function 'browse-url-chromium)


I set the basic key bindings as documented in the Guide, plus configure the M-RET behavior.

`(global-set-key "\C-cl" 'org-store-link)`


(global-set-key "\C-ca" 'org-agenda)

(global-set-key "\C-cc" 'org-capture)

(global-set-key "\C-cb" 'org-iswitchb)

(setq org-M-RET-may-split-line nil)


**Configuration: Capturing**

I can press `C-c c` from anywhere in Emacs. It will [capture something for me](https://orgmode.org/guide/Capture.html#Capture), and include a link back to whatever I was working on.

You can define [capture templates](https://orgmode.org/guide/Capture-templates.html#Capture-templates) to set how this will work. I am going to keep two journal files for general notes about meetings, phone calls, etc. One for personal, one for work items. If I press `C-c c j`, then it will capture a personal item. The %a in all of these includes the link to where I was (or a link I had stored with `C-c l`).

(setq org-default-notes-file "~/org/tasks.org") (setq org-capture-templates '( ("t" "Todo" entry (file+headline "inbox.org" "Tasks") "* TODO %?\n %i\n %u\n %a") ("n" "Note/Data" entry (file+headline "inbox.org" "Notes/Data") "* %? \n %i\n %u\n %a") ("j" "Journal" entry (file+datetree "~/org/journal.org") "* %?\nEntered on %U\n %i\n %a") ("J" "Work-Journal" entry (file+datetree "~/org/wjournal.org") "* %?\nEntered on %U\n %i\n %a") )) (setq org-irc-link-to-logs t)

I like to link by UUIDs, which lets me move things between files without breaking locations. This helps generate UUIDs when I ask Org to store a link target for future insertion.


(require 'org-id)

(setq org-id-link-to-org-use-id 'create-if-interactive)


**Configuration: agenda views**

I like my week to start on a Sunday, and for org to note the time when I mark something as done.


(setq org-log-done 'time)

(setq org-agenda-start-on-weekday 0)


**Configuration: files and refiling**

Here I tell it what files to use in the agenda, and to add a few more to the plain text search. I like to keep a general inbox (from which I can move, or “refile”, content), and then separate tasks, journal, and knowledge base for personal and work items.

(setq org-agenda-files (list "~/org/inbox.org" "~/org/email.org" "~/org/tasks.org" "~/org/wtasks.org" "~/org/journal.org" "~/org/wjournal.org" "~/org/kb.org" "~/org/wkb.org" )) (setq org-agenda-text-search-extra-files (list "~/org/someday.org" "~/org/config.org" )) (setq org-refile-targets '((nil :maxlevel . 2) (org-agenda-files :maxlevel . 2) ("~/org/someday.org" :maxlevel . 2) ("~/org/templates.org" :maxlevel . 2) ) ) (setq org-outline-path-complete-in-steps nil) ; Refile in a single go (setq org-refile-use-outline-path 'file)

**Configuration: Appearance**

I like a pretty screen. After you’ve gotten used to org a bit, you might try this.

(require 'org-bullets) (add-hook 'org-mode-hook (lambda () (org-bullets-mode t))) (setq org-ellipsis "⤵")

**Coming up next…**

This hopefully showed a few things that org-mode can do. Coming up next, I’ll cover how to customize TODO keywords and tags, archiving old tasks, forwarding emails to org-mode, and using git to synchronize between machines.

You can also see a [list of all articles in this series](https://changelog.complete.org/archives/tag/emacs2018).

**Resources to accompany this article**

[org-mode compact guide](https://orgmode.org/guide/)[Full org manual](https://orgmode.org/org.html)[List of tutorials](https://orgmode.org/worg/org-tutorials/)[Carsten Dominik’s excellent Google Talk on org-mode](https://www.youtube.com/watch?v=oJTwQvgfgMM)- A great intro:
[“My workflow with org-agenda”](http://cachestocaches.com/2016/9/my-workflow-org-agenda/) [A more detailed intro](http://doc.norang.ca/org-mode.html)[David O’Toole’s org-mode tutorial](https://orgmode.org/worg/org-tutorials/orgtutorial_dto.html)[Getting Started with org-mode video by Harry Schwartz](https://www.youtube.com/watch?v=SzA2YODtgK4)(another excellent watch). He also has his[example config files](https://github.com/hrs/dotfiles/blob/master/emacs/.emacs.d/configuration.org)available.

I *love* org. I have used it as my main tool for basically anything for around five years. I don’t use much of its organization tools (perhaps I should)… But I love it for the ease to author complex texts. I have so far written one text book, two theses, two book chapters, and tens of smaller pieces. Having all the power of LaTeX with none of its pain is great :-]

Wow, that’s impressive. I figured I’d have to go back to raw LaTeX for more “serious” things at some point. But, I guess, I would have never used Beamer manually and yet here it just works so easily. I can see the structure of the whole presentation and muck about with it quite nicely.

Excellent org-mode series! I’m in the process of learning emacs and org-mode as a total newbie. This seems like a really dumb question, but it’s the one thing I can’t seem to find a clear answer to (or I’m just missing it)… Anyway, where do all these custom configuration settings go? Where is the emacs config file? Is there one by default or do I create one? And if I create it, where does it go?

No problem! You can create it, and this page https://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html describes the various locations you can put it.

Thank you John! That seems way simpler than I was making it. LOL! The learning curve for emacs is quite steep due to the VAST amount of info and custom configs out there. Everyone has their own “opinion” as to how it should be configured. :)

One of my goals (eventually) is to get away from using Evernote for notes & document management. I’d love to read more about how you managed that if you’re ever feeling so inclined to write another post. Currently, I don’t see how one could possibly manage PDF files and/or other scanned documents using emacs. Evernote contains my life — business receipts, tax documents, medical info, TONS of tech KB stuff, and so on. Evernote is awesome, but I’m no longer comfortable having all my info in a proprietary system that could disappear one day with zero warning.

Emacs is a bit like an operating system or a phone in the sense that it’s more a base to start from rather than something completed. There is no one “right” way to configure it.

In my own setup, I use gscan2pdf (which integrates Tesseract OCR) to scan receipts, records, etc. I keep these in a directory tree outside org-mode. Mostly they are written much more than they are read, and this works fine. I can always link to one if I need to from within org-mode.

org does have a notion of attachments.

I’ve been using ikiwiki for KB stuff, though I am possibly going to migrate that to org as well.

I tried the commands in your appearance section, and got this error:

Warning (initialization): An error occurred while loading ‘c:/org/.emacs.d/init.el’:

File error: Cannot open load file, No such file or directory, org-bullets

Ahh, you’ll need to install the org-bullets package. Sorry about that oversight. See https://www.emacswiki.org/emacs/InstallingPackages for details on how to install packages.

Thanks John – I really appreciate the help!

I tried M-x list-packages but I don’t see org-bullets (or any variation) listed. I’ll keep poking around to see if there is another way to install it.

Well, I stand corrected a SECOND time. org-bullets comes with org-mode, at least on Debian.

I had to add this:

(add-to-list ‘load-path “/usr/share/org-mode/lisp”) ; for bullets

It’s at https://github.com/jwiegley/org-mode/tree/master/contrib/lisp and can be added to the load path if need be.

Cool – thanks again! I guess we’re all learning a few things! LOL :)

I’m just taking baby steps at this point. Trying to learn all the key-bindings in emacs and switching over to org-mode for my todo’s. I’ll add more as I get comfortable with each thing.

Emacs didn’t do any good for me because of the stupid key bindings that are too painful for my wrists