Emacs

From AdminWiki

Revision as of 01:55, 6 June 2006 by Fanopanic (Talk | contribs)
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.

Contents

Introduction

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.

Installation

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).

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. Here's mine: http://amd.co.at/fanopanic/dotemacs-0.0.2.tar.gz You should replace every occurrence of "dot" in file/directory names with a literal dot, and move it into your homedir. Typically emacs should accept this configuration on any Unix with X Window System. Under Windows editing is probably necessary.

Modes

The behaviour of emacs is defined by major and minor modes. Major modes alter it significantly, e.g. Lisp, Prolog, Makefile, ... mode, and only one can be active at a time. Minor modes are generally less unobtrusive, e.g. a spellchecker, or an auto-completer.

Commands/Functions

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. In most cases ALT is your Meta key. ESC serves as an alternative Meta key. From this approach it follows that all keybindings can be changed.

Basics

C stands for Control M stands for Meta (usually ALT) H stands for Hyper (normally not used, you can map that useless CAPSLOCK onto Hyper with xmodmap)

Also note that the dotemacs package above contains a more elaborate keybindings cheatsheet.

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 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 vertically C-x 3 split the current window horizontally 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 mapped to 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 mouse 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.

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

Personal tools