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 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.

This is not a joke. It is not about religion or 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. There is one built into Emacs, for example.

Finally, it is not complete and I welcome contributions.

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.

Many Emacs commands take numerical prefixes, but you need to hold down Meta while typing them. So, for example, type M-2 M-3 M-g g instead of 23G to go to line 23.

Commands

Program operations

ActionviEmacsNotes on Emacs
Exit program:q EnterC-x C-cIf changes exist, will ask whether or not to save them.
Unconditionally exit program:q! EnterC-x C-cJust say no.
Save current buffer and exit program:wq EnterC-x C-s C-x C-c
Cancel commandCtrl+cC-g
Redraw screenCtrl+lC-l
Command line:M-x

File operations

ActionviEmacsNotes on Emacs
Open file, or create named buffer:e filename EnterC-x C-f filename
Save current buffer:w EnterC-x C-sWill not save a new, unmodified file.
Save current buffer under new name:w filename EnterC-x C-w filename
Move to next buffer:n EnterC-x b buffer
Move to previous buffer:prev EnterC-x b Enter
ActionviEmacsNotes on Emacs
Go to beginning of buffer1GM-<
Go to end of bufferGM->
Go left one characterhC-bWill wrap to previous line.
Go right one characterlC-fWill wrap to next line.
Go up one linekC-p
Go down one linejC-n
Go to line nnGM-g g n Enter
Go to beginning of line0C-a
Go to end of line$C-ePlaces cursor one step beyond last character.
Go to next wordwM-fStops at first non-word character before the word.
Go to previous wordbM-b
Go to next pageCtrl+fC-v
Go to previous pageCtrl+bM-v
Set mark xmxC-x r Space x
Go to mark xxC-x r j x
Go to first displayed lineH
Go to last displayed lineL
Move buffer one line upCtrl+yC-1 M-v
Move buffer one line downCtrl+eC-1 C-v

Text editing

ActionviEmacsNotes on Emacs
Insert textiAlways in insert mode.
Append textaC-fOnly cursor movement is needed.
Insert at beginning of lineIC-aOnly cursor movement is needed.
Append to end of lineAC-eOnly cursor movement is needed.
Delete character forwardsxC-d
Delete character backwardsXBackspace
Change to end of lineCC-kOnly deletion is needed.
Delete to end of lineDC-kRemoves line entirely if empty.
Delete entire lineddC-a C-kFor empty lines.
C-a C-k C-kFor non-empty lines.
Delete word forwardsdwM-dDoes not delete whitespace before next word.
Delete word backwardsdbM-Backspace
Open line aboveOC-o
Open line belowoC-e Enter
Join linesJC-n M-^
Undo last edituC-x u

Yanking and placing

ActionviEmacsNotes on Emacs
Yank lineyyC-a C-kFor empty lines.
C-a C-k C-kFor non-empty lines.
Yank n linesnyyC-Space (move to line below last desired) M-w
Cut n lines downnddM-n C-k
Paste before cursorPC-y
Paste after cursorp

Searching and substitution

ActionviEmacsNotes on Emacs
Search forwards/pattern EnterC-s pattern Enter
Search backwards?pattern EnterC-r pattern Enter
Global replace text:%s/pattern/with/gc EnterM-% word Enter with Enter
C-M-% pattern Enter with Enter

Window operations

ActionVimEmacsNotes on Emacs
Split horizontallyCtrl+w sC-x 2
Split verticallyCtrl+w vC-x 3
Close current windowCtrl+w cC-x 0
Close all but current windowsCtrl+w oC-x 1

Credits

This page was partly inspired by Emacs for Vi Programmers.

Thanks to Kaj, magda, Ian D, LeViMS, C. Warrington, E. Bowler and R. Pereira for Emacs commands.