emacs
Tutorial
emacs
is generally easier to pick up than vi
, though this is open to debate.
For the most part, anything typed in will go into the file at the position the cursor is at.
The exception is if one holds down control
(abbreviated as Ctrl
through the rest of this document), meta (usually alt
), or some combination of the two as something is typed in.
These allow for commands to be entered.
There are tons of commands emacs
has built-in, and emacs
can even be extended with custom commands, though for the purposes of this tutorial we will not be getting into any of that.
Generally, for your purposes, there are only a few commands that are necessary, which are described below.
In all these commands, the notation Ctrl-foo
means, “Hold down the control key, and type in foo
while holding down the control key.”
The notation a b
means, “Type in 'a' followed by 'b'.”
emacs
CommandsCtrl-a
: Moves the cursor to the start of the current lineCtrl-e
: Moves the cursor to the end of the current lineShift-Ctrl-,
: Moves the cursor to the start of the current file.
Technically, this command is Ctrl-<
, but shift needs to be held in order to enter <
.
Shift-Ctrl-.
: Moves the cursor to the end of the current file.
Technically, this command is Ctrl->
, but shift needs to be held in order to enter >
.
Ctrl-k
: Delete the text from the cursor extending to the end of the lineCtrl-y
: Paste the last deleted text at the current cursor positionShift-Ctrl--
: Undo the last action.
Technically, this command is Ctrl-_
, but shift needs to be held in order to enter _
.
Ctrl-x s
: Saves the current fileCtrl-xc
: Closes emacs
Ctrl-s foo
: Search for the string “foo
” in the current file.
The search starts from wherever the cursor is currently at, and moves forward through the file, stopping at the end.
Note that it will not wrap through the start of the file, which is the same behavior as all the commands described here.
If you wish to go through the entire file, move the cursor to the start of the file first.
Subsequent presses of Ctrl-s
will jump the cursor to different instances of &ldquofoo
”.
Moving the cursor around (as with the arrow keys) will return behavior to normal, as in, search will no longer be occurring.
Ctrl-r foo
: Much like Ctrl-s foo
, except the search moves backward from the cursor.
This does not mean that it looks for the reverse of “foo
”, but rather that it searches through the part of the file from the start to the cursor, starting closest to the cursor.
Hitting Ctrl-r
again will go through different instances.
Hitting Ctrl-s
will behave as if Ctrl-s foo
had been typed in from the current cursor position.
That is, Ctrl-r
can be used to move backwards through different instances of “foo
”, whereas Ctrl-s
can be used to move forward through different instances of “foo
”.
Meta-x replace-string
: Can be used to find and replace one string with another, automatically.
Information regarding what strings are replaced comes up in a dialog at the bottom of the terminal (referred to as the “minibuffer” in emacs
parlance).
Meta-x query-replace
: Like Meta-x replace-string
, except the user is asked whether they want to replace or not on each match.
This process starts from wherever the cursor is in the file, and stops at the end of the file.
Press 'n
' to skip to the next match (short for “no”), 'y
' to replace the current match (short for “yes”), and Ctrl-g
to cancel.
There are actually more possibilities here than described; hit '?
' to see a listing of them.
Prepared for Computer Science 64 by Kyle Dewey, with inspiration from a similar
vi
tutorial by Prof. Diana Franklin.