From AdminWiki

Jump to: navigation, search

Emacs is arguably the most powerful text editor in existence. It's power is derived from it's extensibility via Elisp.



First of all: Emacs is not for everyone, and not for every task. In order to change a few characters in a textfile you are probably better off with a conventional editor like vim. Emacs is more like a multi-purpose development environment that you start in the morning and shut down in the evening, if at all.


Graphical and text-only versions exist, I strongly recommend the graphical version because of a lot of added features. The main distributions of emacs are "GNU Emacs" and "xemacs". I recommend GNU Emacs. Make sure you do not grab the "nox" (no X Window System support) packages, unless you really want to (emacs-nox is actually contained in the graphical package). You should also install ispell, if you want spell-checking to work.

Getting Started

Without customization emacs is no better than any other editor. So in order to get started I suggest you grab yourself a .emacs (e.g. from http://www.dotfiles.com/).


The behaviour of emacs is defined by major and minor modes. Major modes alter it significantly, e.g. modes for specific programming languages that introduce additional keybindings and lots of new functions, and only one can be active at a time. Minor modes are generally less powerful, e.g. a spellchecker, or an auto-completer, and several of them can be active.


In Emacs _everything_ you can do is actually a function that is called when you type a specific key sequence, even trivial things like "Cursor up" are bound to a function, in this case "previous-line". So when you forgot the key sequence for a command you can call the function directly using Meta-x (usually written M-x) and then typing the functionname (see below for Meta). The mini-buffer you are typing this in supports tab-completion. After you ran a function emacs will tell you if you already mapped the function to a key sequence. From this approach it follows that all keybindings can be changed.


C stands for Control
M stands for Meta. Usually ALT. ESC can also be used. The difference is that you need to hold Alt, but you only need to hit and release ESC).
H stands for Hyper (normally not used, but you can map that useless CAPSLOCK onto Hyper with xmodmap, so you have one additional Modifier-key for commands)

Please note that what I cover here is only a small subset of commands and key bindings. You really should read the emacs manual or other resources for the real deal.

C-x C-c  quit
C-x C-s  save
C-x C-f  open ("find") file
C-g      aborts any keysequence/command you started
C-x C-u  undo last action. There is no explicit 'redo' action. Just move the cursor a bit in a direction and then undo again.

You can open an arbitrary number of files. Switching between them means switching buffers.

C-x b    switch to buffer (defaults to previous)
C-x k    kill a buffer (defaults to current)
C-x C-b  show buffer list

As soon as you opened the buffer list you will have noticed that emacs supports split-screens, or rather multiple windows. A window is not the same as a buffer. A buffer resides in a window. When you kill/merge the window the buffer does not die with it. Instead it just stays in the buffer list.

C-x o    switch to 'o'ther window (cycles through. Of couse only when you have more than one open)
C-x 1    kill all windows except the current

C-x 2    split the current window horizontally
C-x 3    split the current window vertically
C-x 0    merge the current window with the adjacent one (if there are two adjacent, then the more recently created one will be merged)

This is very handy in programming. On a decent-sized monitor you can have 4 windows open, and e.g. have 3 source files + 1 terminal open for compilation. You could do the same by arranging xterms, but that's more work and not as comfortable.

M-x term opens a terminal emulator in the current window

Be advised! When in a terminal C-x is C-c! So if you want to switch to a different window use C-c o. This is simply because C-x is used in some applications that may run in the terminal (e.g. irssi uses C-x to switch servers).

Slightly more advanced stuff

C-a      moves to beginning of line
C-e      moved to end of line
C-k      kills a line from the cursor position on
C-y      yanks whatever you killed/copied before. Of course you can yank multiple times!

There is no explicit copy-line command. However you can easily write one yourself (consists of C-a C-k C-y).

C-space  sets the 'mark'. When you move the cursor now you are marking a 'region'. This is very similar to vim's visual mode.
C-w      kills a region
M-w      copies a region (don't ask me why they have made a copy-region command, but no copy-line command)

Yanking region text works as usual with C-y. If you set the mark unintentionally, just use C-g to abort.

comment-region and uncomment-region are very handy. They know from the active major mode what programming language you are using, and use the correct comment sign.

query-replace and query-replace-regexp are used to replace text in a region. They ask you for confirmation at every substitution, use ! to accept all (or use replace-string and replace-regexp in the first place).

One more thing: Flydabbrev is the thing that gives you completion suggestions all the time. You can accept a suggestion and cycle through them by hitting M-/ repeatedly.

Going further

I hope the introduction above helped. I usually don't use a lot more than listed here myself (except for specific languages).

Read the manual at http://www.gnu.org/software/emacs/manual/
Read the .emacs and get other .emacs
Experiment with modes and Elisp. It is a full-fledged programming language, and one of the most powerful.
Read the documentation for individual modes. Modes can do really useful stuff. For example you can send a block of code directly to an interpreter, which pops up in a new window below the code with a single keystroke. That's a lot faster than typing 'make' in a seperate terminal.

Personal tools