Emacs Basics

It’s been a while since I wrote my Vim Introduction and Tutorial (exactly one year). A lot happened between now and then, I chose to get a better feeling about Emacs for example.

The reasons aren’t easily explained; The most prominent reason is the awesome AucTex-mode since I’m working heavily with LaTeX lately.

Anyways, learning Vim and Emacs is better than learning only one of them :-).

Vim started in 1991 and is based on Bill Joy’s Vi written in 1976/77. Emacs started as set of macros for the TECO editor 1976 and was rewritten in 1984 (GNU Emacs). So both editors are based on concepts invented more than 30 years ago and both editors are very unlikely to change any time soon.

I still like Vim’s way of doing things, for example the basic commands are very easy and very fast to reach.

At first I wanted to compile something similar to my Vim Tutorial, but I ended up publishing just a few notes and more or less a cheat-sheet for Emacs. If you like it more official look at the Emacs Guided Tour and its very fine documentation.

The best way to get up and running with Emacs is the included tutorial (start by typing C-h t).

I’m using Aquamacs on my Mac and GNU Emacs on Linux. Both use GNU Emacs 22 at its core, but especially Aquamacs provides some extensions to make it (almost) seamless on OS X.

Basic Concepts

You can’t learn Emacs and lean back, every mode Emacs provides offers its own key bindings and has its own specialities you have to know.

So, it doesn’t matter if you use Vim for day-to-day tasks and Emacs for special purpose text editing (like typing LaTeX, or interacting with some math software). Only the most basic commands are the same for all modes.

Two things to remember

You can cancel any operation either by pressing C-g or three times <Esc>.

To exit Emacs, type C-x C-c.

Major/Minor Modes

A Major Mode provides Key-Bindings, Indentation, Display, Syntax etc. There can be only one Major Mode active at a given time. How would you mix two different syntax-highlighting schemes?

A Minor Mode provides additional bindings and some other behavior. For example spell-correction, automatic line-breaks etc.

Emacs Lisp functions and key bindings

You can access all functions of Emacs with: M-x (that’s <Alt>-x or <Meta>-x or <Esc> x or <Option>-x depending on your setup, preferences and terminology).

C-x means pressing Control and x at the same time, <Esc> x means pressing Escape and x after another.

There is no way to bind all functions to some key-combo and it might be impossible to remember the bindings. So, if you want to replace some string with some other string type “M-x repl<Tab><Tab>” and Emacs gives you possible completions, “replace-string” sounds good.

Emacs’ Architecture

Emacs has a small Lisp core written in C. But the “meat” of Emacs is the expansion-library written in Emacs Lisp. Essentially everything you do and write to extend and configure Emacs is written in Emacs Lisp.

The Bookworm has an introduction to the Emacs introductions.

Undo/Redo

To undo almost every action you did, you can use either undo as command or one of the following key-bindings: C-/, C-_ or C-x u

  • Undo → C-/

To redo, you have to switch the “undo-direction” with C-g, after that you can use the usual undo-key of your choice.

  • Switch undo-direction → C-g

Basic Movement

  • Character movement
    • Up → C-p
    • Down → C-n
    • Left → C-f
    • Right → C-b
  • Page movement
    • Page Down → C-v
    • Page Up → M-v
  • Line movement
    • Start of Line → C-a
    • End of Line → C-e
  • Sentence movement
    • Start of Sentence → M-a
    • End of Sentence → M-e
  • Paragraph movement
    • Start of Paragraph → M-{
    • End of Paragraph → M-}
  • Other
    • Start of File/Buffer → M-<
    • End of File/Buffer → M->

Search and Search & Replace

Searching/Replacing

  • Incremental Search forward → C-s
  • Incremental Search backward → C-r
  • Incremental Search forward with regular expressions → search-forward-regexp
  • Incremental Search backward with regular expressions → search-backward-regexp
  • Replace some string → replace-string
  • Replace using regular expressions → replace-regexp

Text Surgery

  • Deleting/Killing
    • Delete char forward → C-d
    • Delete char backward → <Backspace>
    • Kill word forward → M-d
    • Kill word backward → M-<Backspace
    • Kill to end of line → C-k
    • Kill to end of sentence → M-k
  • Transpose (switch elements)
    • Transpose chars → C-t
    • Transpose words → M-t
    • Transpose lines → C-x C-t
  • Case-operations
    • Capitalize word → M-c
    • Uppercase word → M-u
    • Lowercase word → M-l
  • Text-completion → M-/ (repeat to cycle through possible completions)
  • New-Lines
    • New line → <Return> or C-m (insert a CR before the cursor)
    • Open new Line → C-o (insert a CR after the cursor)
    • New line and indent → C-j

I like to format my text so that no lines are longer than 78 characters. Either I use the minor mode auto-fill-mode or I use M-q to re-justify (fill) the current paragraph (necessary if you edit a paragraph after it was auto-filled).

Cut/Copy/Paste

Basic commands

Maybe you noticed that I changed the terminology of deleting a character to killing a word. The difference is, that “killed” text is put into the Yank-Ring. From this ring, you can get your text back (pasting, yank). The size of your yank-ring depends on your setup.

  • Yank → C-y, use M-y to cycle through the various text-parts.

If you’ve marked some text, use C-w to cut the text, or M-w to copy the text into the yank-ring:

  • Cut → C-w
  • Copy → M-w

Regions

To mark some text, use the mouse, or set a mark with C-<Space> and move the cursor to the end point of the region. The text between the mark and the current cursor-position (also called point) is the region.

  • Set mark → C-<Space>

You can jump between point and mark using C-x C-x, this switches the mark and the point, so that your region remains intact.

  • Switch point and mark → C-x C-x

If you’ve enabled the transient-mark-mode Emacs highlights the region when you create it. As soon as you move the cursor, the region is still there, but the highlight is gone (verify with C-x C-x for example).

Rectangles

The mark and point also specify a rectangle over your text (upper right and lower left edge).

  • Kill the rectangle → C-x r k
  • Yank the rectangle → C-x r y
  • Insert space into the rectangle → C-x r o
  • Replace text of the rectangle → C-x r t
  • Clear rectangle (replace with space) → C-x r c

There is an interesting minor mode for additional rectangle-functionality called CUA.

Frames, Windows and Buffers

Emacs has the concept of Frames, Windows and Buffers. A Frame is a Window in the common sense, a Window is a part of the frame. For example if you split the screen of your Emacs frame, you got two windows within one frame.

The last concept is the Buffer, it’s simply a real or virtual file (virtual == not saved to disk).

Buffer commands:

  • Load a file (buffer) → C-x C-f
  • Show loaded buffers → C-x C-b
  • Switch to another buffer → C-x b
  • Close a buffer → C-x k
  • Save a buffer → C-x C-s

Frame commands:

  • Open new frame with file → C-x 5 f
  • Close this frame → C-x 5 0
  • Switch to other frame → C-x 5 o

Window commands:

  • Split window vertically → C-x 2
  • Split window horizontally → C-x 3
  • Close all other windows → C-x 1
  • Close this window → C-x 0

The Minibuffer (the last line of your Emacs screen) is a buffer
itself, but has some specialities I wont go into now.

Getting Help from Emacs

You could fill some very big books with Emacs-specific information, but Emacs calls itself a “self-documenting” editor. This means that the documentation reflects exactly how your Emacs is configured. If you change some key-bindings, the documentation reflects this changes and displays the right key-combo in the help files.

Some hints on how to get going using the help-system:

  • Show information on the current mode(s) → C-h m
  • Describe the command bound to the given key → C-h k
  • Search for a function → C-h a
  • Describe all key-bindings of the current mode(s) → C-h b

Further Information

Steve Yegge has some intersting articles on Emacs:

IBM has some nice Emacs tutorials too.

The EmacsWiki is another excellent source for Emacs related howtos, downloads and – to some extend – discussions.

Wish

I’d wish that Vim and Emacs would merge. That I could take advantage of Emacs’ awesome tool-support and Vim’s awesome way of editing small junks of text. I know there is Viper-mode and Vimpulse, but both are not exactly what I want. Viper-mode is plain Vi, and Vimpulse is early in development.

Is there a way to integrate a Vim window into Emacs without emulating and reimplementing the features?

For the time being, I’ll have to switch between the environments. Vim as my default-editor and Emacs for special purpose editing.

Emacs outshines all other editing software in approximately the same way that the noonday sun does the stars. It is not just bigger and brighter; it simply makes everything else vanish.
— Neal Stephenson

Comments

comments powered by Disqus