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 about religion, and I will not present one editor as better than the other, nor is it about the Emacs mindset, since I have yet to acquire it myself. It is, however, about familiarity, 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 specific to Emacs. I believe there to be sufficient overlap between the capabilities of each editor to motivate this approach.

This page also isn't a proper Emacs tutorial; rather it's a basic survival guide for the Vi user. If you want to learn Emacs propely, go find a proper tutorial.

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 vi 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 N/A
Cancel command Ctrl+C C-g N/A
Redraw screen Ctrl+L C-l N/A
Command line : M-x N/A

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
?

Go to mark x
'x
?

Go to first displayed line
H
?

Go to last displayed line
L
?

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-k
C-k C-k
For empty lines.
For non-empty lines.
Cursor must be placed at beginning of line, use C-a if needed.
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
C-a C-k C-k
For empty lines.
For non-empty lines.
Also deletes the yanked text.
Yank n lines
nyy
C-Space (move to line below last) M-w

Cut n lines
ndd
C-Space (move to line below last) C-w
Paste before cursor
P
C-y

Paste after cursor
p
?

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 and LeViMS for Emacs commands.