Emacs for vi users

"Why can't I enter command mode?"

Introduction

This is intended as an introductory guide for vi users wishing to learn the basics of Emacs. I'm writing it because I'm one of them, and I suspect that I'm not alone in being mystified by the lack of a command mode, text objects and an underlying ex editor, as well as the plethora of strange and unfamiliar key combinations. The intent is to give fledgling Emacs users a basic set of commands, sufficient for basic editing.

This page is not a joke. It is not about religion. It does not condone the Editor war and I will not present one editor as better than the other. Neither is it about the Emacs mindset, since I have yet to acquire it myself. Finally, it is not a proper Emacs tutorial. If you want to learn Emacs propely, go find a proper tutorial.

It is a basic Emacs survival guide for the vi user. It is grounded in familiarity of vi and (quite naturally) assumes a vi-centric view of the world. Since the structure is that of a list of corresponding commands, I will not list features that are unique to Emacs. I believe there to be sufficient overlap between the capabilities of each editor to motivate this approach.

Basics

Emacs doesn't have a separate command mode. In a sense, you're always in insert mode. Therefore, special modifier keys are needed to tell commands apart from typed characters. The two commonly used modifiers are Control and Meta. On many systems, the Alt key can be used as Meta. If your system lacks a functional Meta key, you can type Escape before the specified key.

Since Control and Meta are so frequently used in Emacs, they have created a special convention for writing such commands, so for example C-a corresponds to Control+A, and M-f corresponds to Meta+F. The Emacs command column will use this convention. This is also consistent with the documentation in Emacs.

Commands

Program operations

Action gi Emacs Notes on Emacs
Exit program :q C-x C-c If changes exist, will ask whether or not to save them.
Unconditionally exit program :q! C-x C-c Just say no.
Save current buffer and exit program :wq C-x C-s C-x C-c
Cancel command Ctrl+C C-g
Redraw screen Ctrl+L C-l
Command line : M-x

File operations

Action vi Emacs Notes on Emacs
Open file, or create named buffer :e filename C-x C-f filename
Save current buffer :w C-x C-s Will not save a new, unmodified file.
Save current buffer under new name :w filename C-x C-w filename
Move to next buffer :n C-x b buffer
Move to previous buffer :prev C-x b Enter

Navigation

Action vi Emacs Notes on Emacs
Go to beginning of buffer 1G M-<
Go to end of buffer G M->
Go left one character h C-b   or   Left Will wrap to previous line.
Go right one character l C-f   or   Right Will wrap to next line.
Go up one line k C-p   or   Up
Go down one line j C-n   or   Down
Go to line n nG M-x goto-line Enter n
Go to beginning of line 0 C-a
Go to end of line $ C-e Places cursor one step beyond last character.
Go to next word w M-f Stops at first non-word character before the word.
Go to previous word b M-b
Go to next page Ctrl+f C-v   or   PageUp
Go to previous page Ctrl+b M-v   or   PageDn
Set mark x mx C-x r Space x   or   M-x point-to-register Enter x

Go to mark x 'x C-x r j x   or   M-x register-to-point Enter x
Go to first displayed line H Unknown
Go to last displayed line L Unknown
Move buffer one line up Ctrl+y C-1 M-v
Move buffer one line down Ctrl+e C-1 C-v

Text editing

Action vi Emacs Notes on Emacs
Insert text i Always in insert mode.
Append text a C-f Only cursor movement is needed.
Insert at beginning of line I C-a Only cursor movement is needed.
Append to end of line A C-e Only cursor movement is needed.
Delete character forwards x C-d   or   Delete
Delete character backwards X Backspace
Change to end of line C C-k Only deletion is needed.
Delete to end of line D C-k Removes line entirely if empty.
Delete entire line dd C-a C-k   or   C-a C-k C-k The former for empty lines, the latter for non-empty lines.
Delete word forwards dw M-d Does not delete whitespace before next word.
Delete word backwards db M-Backspace
Open line above O C-a Enter C-p
Open line below o C-e Enter
Join lines J C-n C-a Backspace
Undo last edit u C-x u

Yanking and placing

Action vi Emacs Notes on Emacs
Yank line yy C-a C-k   or   C-a C-k C-k The former for empty lines, the latter for non-empty lines.
Yank n lines nyy C-Space (move to line below last desired) M-w
Cut n lines ndd C-Space (move to line below last desired) C-w
Paste before cursor P C-y
Paste after cursor p Unknown

Searching and substitution

Action vi Emacs Notes on Emacs
Search forwards /pattern C-s pattern
Search backwards ?pattern C-r pattern

Credits

This page was partly inspired by Emacs for Vi Programmers.

Thanks to Kaj, magda, Ian D, LeViMS and C. Warrington for Emacs commands.