Emacs Introduction

Introduction

Emacs is a powerful text editor designed for programmers. This quick introduction and reference guide is target at the new or casual user.

The following notation is used throughout:

C-
means hold down the control key and type the following character. For example, C-x means hold down the control key and type x.
M-

means hold down the meta key and type the following character. The meta key is usually on the bottom left of the keyboard next to the spacebar.

This will only work on keyboards with a meta key.

However, on all keyboards you can use the ESC key. That is, hit the ESC key (DO NOT hold it down) and then the following key.

\cr
RETURN (or carriage return),

Two very important commands to remember as you learn Emacs are:

C-g
Quit. Cancel running or partially typed command.
C-_
Undo the last change made. If this command is repeated it will undo each previous change.

Starting and Stopping Emacs

To enter the Emacs editor simply type emacs. To start Emacs with a particular file you can type: emacs filename.

Note: you may need to add some options to avoid opening a new X window. I typically run Emacs as:

emacs -nw --color=no

To begin working on a new file, say newfile, just type emacs newfile and the file will be created.

To exit Emacs you need to use the control sequence: C-x C-c

NOTE: it asks you in turn if you want to save each file that has been edited. If you do not save a file then any changes you made will be lost!

Backup files

Emacs automatically creates backup files whenever a file is changed. When a file is opened a copy is stored in the file: filename~

If Emacs is aborted for any reason it will store the changed files to recover files in: #filename#

Whenever a file is opened, if there exist a #filename# that is newer than filename emacs will tell you:

Auto save file is newer; consider M-x recover-file

You can then use the command:

M-x recover-file \cr filename

to recover the file.

Files

C-x C-f *
read a file into a Emacs buffer
C-x C-s
save a file back to disk.
C-x i
insert contents of another file into the current buffer

NOTE: The * means you will be prompted for further information and you must end the information with \cr.

Moving The Cursor

C-b
move back one character
C-f
move forward one character
C-n
move down to the next line
C-p
move up to the previous line
C-a
move to the beginning of the line
C-e
move to the end of the line
C-v
scroll forward one screen
M-v
scroll backward one screen
M-<
go to beginning of buffer
M->
go to end of buffer
M-x goto-line \cr *
goto the given line number

Search And Replace

In Emacs you can use the following commands to find and replace string patterns. When you use these commands, a prompt will appear at the bottom of the screen (referred to as the mini-buffer) where you enter the string.

C-s *
Incremental search forward
C-r *
Incremental search backwards

Incremental search means that it will search for the characters as you type them. For example, if you typed find it would first find the next f, then it will find fi followed by fin and finally find.

M-x replace-string \cr *
Replace every occurence of an old string with a given new string
M-% *

query if every occurence of an old string should be replaced with a given new string

Each time you are prompted in the mini-buffer you can reply:

SPC or y
yes, replace this string
DEL or n
no, do not replace this string but continue
ESC
no, do not replace this string and quit
!
yes, and replace all the rest without asking.

M-% is very useful because the user can check if the replacement is correct and try again if it is not.

Deletions and Cut And Paste

In Emacs you can use the following commands to cut and paste text. Cut and paste commands involve the concept of a region.

A region is the area of text between the cursor and the last mark that was set.

C-SPACE
set the mark at the current position
C-@
also sets the mark
C-x C-x
switch the mark and the cursor (to check what the bounds of the region are)
C-w
Wipe out a region and store it in the kill buffer (cut)
M-w
Do not wipe out a region but still store it in the kill buffer (copy)
C-y
Yank last killed text (i.e., put it back in the text at the present cursor location) (paste)
C-k
kill from cursor to end of line and store the text in the kill buffer
DEL
delete last character (i.e., to the left of the cursor)
C-d
kill/delete next character (i.e., under the cursor)

Multiple Windows

When you first run Emacs you open one window and can only work on one thing at a time.

However, it is possible to work on two parts of the same file (for example to move --- or copy --- text from one part to the other) or to work on two different files at the same time.

To understand this you need to understand buffers.

The text you are editing in Emacs resides in an object called a buffer. Each time you visit a file (i.e., do emacs filename or C-x C-f filename) a buffer is created to hold the file's text.

At any time, one and only one buffer is selected (called the current buffer). If there are two or more buffers in use at the same time you can change the current buffer by C-x b buffer name.

By typing C-x 2 you split the one window into two windows --- each of which will show the same text. You can move each window to show a different part of the same buffer; then any change in one window changes the contents of the buffer and so the change also applies to the other window.

However, if each window is showing a different buffer then each window is completely independent. Text can still be copied or moved from one buffer to the other by cutting and pasting.

To do this you would split the window into two windows and then in one of the windows load another file. You can then move between the buffers with the C-x o command.

C-x 2
split the window into 2 windows (you will get an error if window is too small to split)
C-x o
switch cursor to the another window (if more than one exists)
C-x 0
delete this window (but the file still remains)
C-x 1
delete all windows but this window
C-x b *
work on a different buffer in this window
C-x k
kill a buffer (the file is not killed but it is no longer in Emacs's buffers)
C-x C-b
display a buffer list window showing all the currently loaded buffers (most recently visited at the top of the list).

Emacs keeps track of what you are doing, and offers its best guess at what you want.

For example when you do C-x b it offers the name of the last buffer you were in as default.

Thus by simply repeating C-x b \cr you can toggle repeatedly between two buffers.

Compiling a Program

Emacs is the original Integrated Development Environment (IDE).

M-x compile \cr *
run a compiler in Emacs

Emacs assumes you want to compile by using a make file and types make -k.

You can change the command that will be run, and it will be remembered for next time.

Emacs will split the window into two windows and show the output of the compilation in the second window. Note that Emacs will also ask you if you want to save each file that has been changed. If you do not save a file then the compilation will occur on the unchanged file!

If there are no errors you are done --- if there are errors they can be fixed in turn by typing C-x ` (NOT an apostrophe but a grave symbol --- i.e., a backward apostrophe).

Emacs will visit each file that has have error messages and put the cursor at the line of the next compilation error.

Simply fix this error and type C-x ` to go to the next error.

When all the errors have been corrected, type M-x compile again to recompile the program.


Author:sjg@crufty.net
Revision:$Id: emacs-intro.txt,v 59e0bded5705 2016-10-12 01:26:30Z sjg $
Copyright:Crufty.NET