Emacs quick start: One n00b to another.

I recently got started using Emacs, and I was surprised to see just how bad most of the quick-start guides are. Most lacked the basic commands and definitions that beginners really need. This is my attempt to do better. I'm a n00b myself, so there won't be much explanation, just a basic overview, some useful starter commands, and links to proper tutorials.

NB: Emacs is not one thing. This post is abut GNU Emacs 23.

Most interaction with Emacs (besides normal typing of text) is via the issuance of commands. These commands almost exclusively involve modifiers like Control and Meta, which correspond to Ctrl and Alt on most keyboards. If you see the keyboard shortcut M-x, you should type the [X] key while holding down [Alt]. Similarly, C-h means [Control]-[h]. Some commands involve a sequence of keystrokes. You can remap all of these later if you wish. (You should also familiarize yourself with some other key and character conventions.)

Here are your first commands: C-g will cancel just about anything (a command in progress, a selection, etc.) and C-x C-c will quit the program. Note that C-x in most programs is "Cut to clipboard"—this is not what it is bound to in Emacs. In fact, none of the usual keyboard shortcuts apply. You'll learn about their replacements soon.

Diagram of Emacs start screen

When you first launch Emacs, you will probably see a single window split into two panes, one above the other. (Emacs calls this a vertical split instead of a horizontal split, confusingly, but we'll stick with the convention.) These panes are called windows in the rest of this document. You have a cursor in each one, but only one will be blinking and active. This is the current window. Want to get rid of all windows except the current one? Use C-x 1 to do that. C-x 0 will close the current window.

The stuff you see inside each window is a buffer, which can be anything from a command line to a document to an interactive settings file. You can have a bunch of buffers open at any given time, and each window can be targeted to show any one of them. You will usually just have one window open and keep switching which buffer is being viewed in it. C-x b will give you an interactive prompt that will let you swap out which buffer you are using, and C-x C-b will give you the full list in the current window until you position your cursor over the one you want and hit [enter].

Emacs is always in one major mode at any given time. Major modes generally change with your programming language, and affect syntax highlighting, autocompletion, compilation, etc. There are also minor modes, which can coexist peacefully. Most notably, modes change what certain commands mean. There's a command to move forward by one word, but the meaning of "word" changes between programming languages. (If you're using an s-expression language, for instance, you'll really want the Paredit mode, which lets you sling parens like a master.)

Undo mistakes with C-/. If it really wasn't a mistake after all, you'll still be able to get it back later by undoing some more!

Now that you know the real basics, you're ready to look at all those other Emacs tutorials! Your next steps:

  1. Open and save files
  2. Keyboard navigation (and why the arrow keys can be trouble)
  3. Undo, and redoing by undoing
  4. Selection
  5. Killing and yanking are not just different words for cutting and pasting
  6. Understanding, installing, and enabling modes
  7. How to use the amazing help system (C-h)

I'll add to and modify this document as my own understanding progresses.

Oh yeah, and here's some common stuff that isn't mentioned in regular guides:

  • Trying to save to a new filename that has a space in it, and Emacs isn't letting you? C-q space will put in a literal space.
  • Want to put a newline character in a search or replace pattern? C-q C-j inserts a ^J (line-feed character), which is usually what you want. In DOS mode you'll need to insert an ^M (carriage return) before the line feed, otherwise some programs might freak out.

Responses: One so far

  1. justin says:

    Whoa. I don't know whether to congratulate you or send my condolences.

    Perhaps I'll contribute a few lines from my ~/.emacs instead.

    (global-font-lock-mode t)
    (iswitchb-mode 1)
    (setq iswitchb-buffer-ignore '("^ " "*Buffer"))
    (setq iswitchb-buffer-ignore '("^\*"))
    (global-set-key "\M-s" 'imenu)
    (global-set-key (kbd "M-g") 'goto-line)
    (global-set-key (kbd "M-c") 'comment-region)
    (global-set-key (kbd "M-C-c") 'uncomment-region)
    (require 'mwheel)
    (mouse-wheel-mode 1)
    (tool-bar-mode 0)
    (add-hook 'text-mode-hook '(lambda () (auto-fill-mode 0)))

    (setq
    backup-by-copying t ; don't clobber symlinks
    backup-directory-alist
    '(("." . "~/.emacs-backups")) ; don't litter my fs tree
    delete-old-versions t
    kept-new-versions 6
    kept-old-versions 2
    version-control t) ; use versioned backups

    ;; AucTeX
    (require 'tex-site)
    (add-hook 'LaTeX-mode-hook 'turn-on-reftex)
    (add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
    (setq reftex-plug-into-AUCTeX t)
    (setq reftex-label-alist '(AMSTeX))
    (setq reftex-cite-format 'natbib)