UP | HOME

An Introduction to Emacs

– This introduction borrows heavily from Andy Harris's Emacs Guide, Prima Tech 2000.

Table of Contents

Why choose emacs

  • Emacs is good for powerful text editing
  • Know which editor is good for the job. Emacs might not always be the best choice.
  • Emacs can radically change the way you do certain jobs. You do not yet realize how, because you have most likely adapted to certain inefficiencies in your current workflow.
  • Emacs requires time, but, if you are willing, you will find that it profoundly improves the way you do some of your work.

Emacs Conventions

  • Emacs assumes you are smart. You will have to learn things.
  • Is built to run at 100% within terminal. Do not assume any GUI for emacs when you learn it.
    • Example of -nw vs. GUI.
  • Keyboard commands. Most have reasonable mnemonics. The benefit is that your hands will never have to leave the keyboard to perform extremely complex commands.

Control combinations

  • A combination of the CTRL key and some other key.
  • Remember C-g
  • Multi-key sequences
    • C-x C-f example
    • All file mgmt commands start with C-x

Meta combinations

  • Control combos are not enough to encompass all of emacs' commands.
  • Meta on various systems: I know how mac works, figure out your OS meta key.
    • ESC works as Meta on any system. You don't hold it down though.
  • Meta as amplifier of CTRL.
    • Example, C-f, M-f.
  • M-x and command names.

Help and Documentation

  • Before going online, check emacs for help. It is always better.
  • If you don't know how to ask for help, use C-h ? or C-h C-h
    • C-h c Defines a key combination (eg C-x C-f)
    • C-h i Opens the info pages (Fantastic source for Emacs and Elisp documentation)
    • C-h t Opens the tutorial

Apropos Help (C-h a)

Similar to searching an index. Provides info on any matching command.

Refcards

All emacs installations come with refcards for your convenience. These are pdfs with a list of common commands for certain modes in emacs. In my Mac installation of emacs, I can find these refcards in

/Applications/Emacs.app/Contents/Resources/etc/refcards

They are likely in a similar location in your installation.

Editing

Movement Commands

  • C-f C-b C-p C-n Move Forward, Backward, Previous-line, Next-line
  • M-f M-b Move Forward by word, Backward by word
  • C-v M-v Scroll Vertically down, Scroll Vertically up
  • C-a C-e Go to beginning of line, end of line.
  • M-< M-> Go to beginning of buffer, end of buffer.
  • C-l Center the cursor

Deleting text

  • C-d Delete character to the front of point.
  • M-d Delete word to the front of point.
  • <DEL> Delete character to the back of point (normal).
  • M-<DEL> Delete word to the back of point.

Killing and Yanking

Copy/paste doesn't work the exact same way in Emacs as it does in most editors. At first this might be confusing, but later you will begin to see the advantages of doing it this way.

  • C-k Kill the line after cursor.
  • C-y Yank the last thing that was killed.
  • M-y After using C-y, yanks the last thing that was killed before that, and so on through the kill ring. Cycles through each of the previous things that were killed one at a time.

Marking regions

  • C-SPACE Set/unset the mark. After the first C-<SPACE>, move the cursor to where you want to highlight the appropriate region, then use one of the kill commands below on this region.
  • C-w M-w Kill the current region, "copy" the current region into the kill ring.

Undoing

  • C-_ (That's Control + Underscore, so the keys are Ctrl, Shift, and Minus "-") Undo's the last action

Files, Buffers, and Windows

Files

Actual file on disk. This is only written to upon request.

  • C-x C-f Find file. Will look for a file if it exists, otherwise will create the file for you.
  • C-x C-s Save current buffer to file.
  • C-x s Prompt to save each buffer.
  • C-x C-w Write the current buffer to specified file (same as save-as).

Buffer:

A copy of the file being edited in emacs. The actual file does not change until you ask emacs to save the buffer. You can save a buffer to a different file with

  • C-x b Switch buffer to … (default is the last buffer you had open).
  • C-x k Kill the current buffer.
  • C-x C-b Show list of all current buffers. You can navigate this list to open, kill, save, etc.. on any or some buffers.
  • M-x kill-some-buffers Prompts to kill each buffer currently open.

Window

Where buffers are shown. "Current window" refers to the one your cursor is currently in.

  • Working with multiple windows
  • The difference between windows and frames
    • C-x 0 Delete the current window.
    • C-x 1 Make the current window the only visible one (full-screen it).
    • C-x 2 Split the current window into two horizontally.
    • C-x 3 Split the current window into two vertically.
    • C-x o Move cursor to the next window (order is up to down, left to right).
    • C-M-v scroll-other-window Scroll the "other" window, really useful when you have only two windows.

Frame

Holds windows, only useful with Gris. Not much to be said. There is no concept of a frame when you use Emacs in terminal.

Working with Line Numbers

  • M-x goto-line M-g g Go to the specified line number (it will prompt you for the line you want).

Major Modes & Minor Modes

  • C-h m Display help on current modes
  • C-c The usual key combination prefix before minor-mode specific commands. For instance C-c C-c in C will comment a region.

Advanced Editing

Search and Replace

  • C-s Search for string forwards through current buffer. Each repeated execution of C-s will repeat that search for the same string until you press <RET> or C-g.
  • C-r Same as C-s but goes backwards through your buffer instead.
  • M-x replace-string
  • M-x query-replace-string
  • M-x replace-regexp
  • M-x query-replace-regexp

Keyboard Macros

  • C-x ( Start recording macro
  • C-x ) Stop recording macro
  • C-x e M-x call-last-kbd-macro Execute the last recorded macro.

Remember C-g! It is useful if you want to cancel recording a macro.

  • Example of macro: Indent every other line by two spaces.

Spell Checking

  • Installing ispell
  • Using ispell (M-x ispell-buffer)

    flyspell-mode

Emacs and Unix

Shell

M-x shell

Managing Files and Dirs with Dired

Directory Editor

C-x d, or C-x C-f on a directory

Most editing commands supported, such as C-s

Specific dired commands are a single key.

  • Rename file: R
  • Copy file: C
  • Set permission on file: M
  • Mark files for deletion: d
  • Delete marked files: x
  • Unmark files: u

Remember C-h m. Let's try this now and see what else we can do.

File backups

Emacs automatically creates timely backups of your files. You will see these in the directory of the file being edited.

Backup Files: Previous copy of the file. Tilda (~)

Auto-Save files: Current copy of the file prior to saving explicitly. Gets updated frequently and on system errors. Hashes surround files. #filename#

Author: Mishal Awadah

Date: 2012-11-14 17:49:50 EST

HTML generated by org-mode 7.3 in emacs 24