美思 技術雜談:Unix is Friend - Emacs

Facebook Twitter LinkedIn LINE Skype EverNote GMail Yahoo Email

Many files on Unix are plain text files. Some examples are text files, markup language files, LaTeX files, source code, Makefiles, and configuration files. Unix utilities also works better with text files. Besides, proper editors help us editing text files productively and effectively. Emacs and Vim are the two most famous editors in Unix world. If you want to enjoy good user experiences on Unix, you may start with Vim or Emacs. Here we take a look at Emacs.

Emacs is an extensible, customizable text editor written in Emacs Lisp. There are dozens of major modes and minor modes for different purposes. Emacs detects files and change modes automatically; you may also change and set modes by yourself. Emacs itself is a self-contained system; many things can be done without exiting Emacs. One joke about Emacs is "What Emacs operating system lacks is a decent editor." There are several implementations of Emacs, including GNU Emacs, XEmacs, Aquamacs (on OS X) and other variants. The most famous implementation of Emacs is GNU Emacs.

Although there are many different major and minor modes to learn, the basic operations are the same. Open Emacs and press C-h t to enter the Emacs tutorial. (C-h t means holding Ctrl key and press h key; then, release Ctrl and h keys and press t key). The tutorial is not too long to follow.

The help documents in Emacs are important sources to learn Emacs. Here we list some Emacs commands for help; you may start self learning by these commands. Press C-h ? for help of help, showing all the following commands and more.


C-h t    The Emacs Tutorial
C-h r    The Emacs Reference
C-h C-f  Emacs FAQs
C-h i    Info system in Emacs
C-h m    Help document for current major and minor mode.
{{< / highlight >}}

There are also some help that helps while writing Emacs Lisp:

```text
C-h f    Query function definition
C-h v    Query variable definition
C-h k    Query the command for specific keybinding
{{< / highlight >}}

If operating Emacs help system is difficult for you, there are also online documents [here](http://www.gnu.org/software/emacs/#Manuals).  Using a web browser is simpler than manipulating Emacs :-).  To support Free Software Foundation (FSF) for their great work, consider to buy a print copy of Emacs manual.

Even you are end user, you may need to write a few Emacs Lisp while editing the config file.  To customize Emacs, edit *~/.emacs* to fit your own need.  You don't have to start everything from the scratch.  There have been some solutions on [Emacs Manual](http://www.gnu.org/software/emacs/#Manuals), [Emacs Wiki](http://www.emacswiki.org/), or [StackOverflow](http://stackoverflow.com/).  You may also google some code snippets on the net.

You don't need to memorize all keybinds of all major modes; stick to basic editing keybinds and commands first; pick up more keybindings and commands gradually.  For mainstream text file formats and programming languages, there have been one or more Emacs major mode to handle it.  If the mode is not a built-in part of Emacs, you may get it from a third party package.

Beginning at Emacs 24, installing packages becomes easier.  Type `M-x list-packages` to the Package Menu; choose the packages you want; Emacs will install them for you.  You may add [marmalade](http://marmalade-repo.org/), [MELPA](http://melpa.milkbox.net/), and [org](http://orgmode.org/elpa.html) repositories for more packages.  Edit *~/.emacs* to add the three repositories:

```lisp
(require 'package)
;; marmalade repo
(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/") t)
;; MELPA repo
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)
;; org-mode repo
(add-to-list 'package-archives
             '("org" . "http://orgmode.org/elpa/") t)
{{< / highlight >}}

There are also some useful mode not for editing tasks.  Some of them are:

- Dired mode: a file and directory manager.
- Shell mode: interact with system shell.
- Eshell mode: shell written in Emacs Lisp.
- Calendar and Diary mode: for calendar and to-do list.
- Org-mode: notes management and organizer.

There are even some modes just for fun.  For example, press `M-x doctor` and begin to talk to a psychiatrist now.

You may want to give Emacs a try now.  Installing Emacs on Linux and Unix is easy.  You may use system package manager like `apt-get` or `yum` to get Emacs.  On Mac, download [Aquamacs](http://aquamacs.org/) or use MacPorts or Homebrew to get GNU Emacs.  Good luck.
關於作者

身為資訊領域碩士,美思認為開發應用程式的目的是為社會帶來價值。如果在這個過程中該軟體能成為永續經營的項目,那就是開發者和使用者雙贏的局面。

美思喜歡用開源技術來解決各式各樣的問題,但必要時對專有技術也不排斥。閒暇之餘,美思將所學寫成文章,放在這個網站上和大家分享。