Defindit Docs and Howto Home

This page last modified: Oct 05 2009
title:Emacs key commands for beginners
keywords:emacs,keys,commands,control,escape,beginner,learn,easy,fun,powerful,
description:Quick notes on how to learn and use Emacs in less than five minutes.



Table of contents
-----------------
What is Emacs?
Introduction to Emacs keys
Keys you need to start
Get my .emacs (dot-emacs) init file
Other key bindings


What is Emacs?
--------------

Emacs is a powerful tool often used for the simple task of editing
text files. Emacs is a full Integrated Development Environment (IDE)
which supports many programming languages and file types. In the
simple case, you use Emacs to write Perl and shell scripts. In the
full case, Emacs is a powerful and wonderful tool to edit, compile,
run, debug, and document both code and text format data
files. Beginners can get started with Emacs in a few minutes, learning
only seven commands. Experienced programmers will be using Emacs
decades later because it is one of the most useful software packages
ever created. 

Yegge sums it up in the sixth paragraph down at:

http://steve.yegge.googlepages.com/effective-emacs

Yegge says:

"Compared to Emacs Wizards, graphical-IDE users are the equivalent of
amateur musicians, pawing at their instrument with a sort of
desperation. An IDE has blinking lights and pretty dialogs that you
can't interact with properly (see Item 6), and gives newbies a nice
comfortable sense of control. But that control is extremely crude, and
all serious programmers prefer something that gives them more power."

Emacs is a complex tool, but I think my introduction and hints will
make it easy to get started. Certainly, getting started with Emacs is
easier than vi or vim with the bizarre dual mode notion.



Introduction to Emacs keys
--------------------------

There are only 7 keystrokes (commands, keys) necessary to use Emacs
effectively.

Using emacs requires use of the control and excape keys.
In the notes below, this is how these keys are denoted:

C		Control key
ESC		Escape key, meta, aka M-
Alt		Behaves like a shift-Meta or control-Meta key: depress with a second key

C is "control" and is also "Ctrl" and is called "the control key". For
example C-x means hold down the control key, then press x then
release both keys. This is similar to using the shift key to type
capital X. When showing multiple keystrokes, the keys are separated by
a space. C-x s means do C-x as above, then press s and release.

M is "meta" and is also "ESC" and is known as "the escape key". ESC-x
means press ESC and release, then press x and release. M-x has the
same meaning.

The space bar is called spc or SPC. The enter key is RET (historically
known as the return key).

Use C-g to cancel any running command or any multi-key operation.

The action of a key is called the "binding". 



Keys you need to start
----------------------

key             binding
---             -------

C-g		keyboard-quit (cancel)

Open file:
C-x f		find-file (open file)

Save:
C-x s		save-buffer (save)

Save and exit
C-x C-c		save-buffers-kill-emacs (exit, quit)

To cut:
mark, move cursor, cut
C-SPC, move cursor, C-w

To copy:
mark, move cursor, copy
C-SPC, move cursor, ESC-w

To paste:
C-y		yank (paste)

Use the cursor control keys (arrows), page-up, page-down, home, and
end to navigate (that is: to move the cursor). Running Emacs in a
graphical environment allows you to use the mouse.



Get my .emacs (dot-emacs) init file
-----------------------------------

You should get my .emacs (pronounced "dot emacs") file (if you don't
already have it) and save it in your home directory. These are
improved key bindings. You can modify the .emacs file, and move it
onto all systems where you work.

http://defindit.com/readme_files/tom_emacs.txt

Linux and OSX: download to your home directory and rename to .emacs

Windows: probably put in C:\ and name _emacs or .emacs and see the
documentation "Where do I put my init file" at:

http://www.gnu.org/software/emacs/windows/Installing-Emacs.html

On Linus and OSX, use your favorite package manager (yum, yast, etc.)
to install Emacs. It has a huge amount of functionality, and is
therefore fairly large (70 MB). Under Fedora it seems to consist of 3
packages: emacs, emacs-common, and emacs-release.

For Windows, download the single binary from the GNU ftp site. Unzip
in a logical location (program files). Most people want the full "bin"
which are all binaries and docs (no source).

http://ftp.gnu.org/gnu/emacs/windows/

For example:

http://ftp.gnu.org/gnu/emacs/windows/emacs-22.3-bin-i386.zip



Other key bindings
------------------

The keys above are the minimal commands necessary to use emacs,
assuming you use the use the keyboard arrow keys to move the
cursor. Below is a more-or-less complete list of bindings.

If you do M-x the text "M-x " will appear in
the "mini-buffer" at the bottom of the emacs screen. M-x is the Emacs
command mode, and it is therefore a somewhat special command. Do not
press Enter (return) after M-x, because M-x expects you to enter a
command first. After entering a command you must press the Enter
(return) key for that command to run. Commands will auto-complete if
you press the spacebar. Emacs commands have weird names.

Holding down the Alt key and pressing a second key behaves as
though you pressed ESC, released it, then pressed a second key. For
example, "press and hold Alt, then press x, then release both" is
identical to "press and release ESC, press and release x". The Alt
version is handy for certain scrolling commands.

C-x		Control-x, the Prefix Command (see use below)

Cursor moving:
Use arrow keys or:
C-b		backward-char (move cursor left)
C-f		forward-char (move cursor right)
C-n		next-line (cursor down)
C-p		previous-line (cursor up)
C-a		beginning-of-line (move cursor)
C-e		end-of-line (move cursor)
Alt-n		up-one (scroll page up one line without moving the cursor)
Alt-p		down-one (scroll page down one line without moving the cusor)

Search:
C-s		search-forward
ESC-ESC		repeat-complex-command (repeat last search)



Commands below are extra emacs features, and some review of the 
information above.

C-x C-z		suspend emacs (background emacs, return to shell.
		use "fg" to foreground emacs)

Compile/syntax check Perl:
C-x c  then delete text in minibuffer and
replace with "perl -cw filename.pl" and press Enter/Return to begin compiling.

C-x c		compile
C-x C-n		next-error (go to next error shown in compile results)
ESC g		goto-line

C-w		kill-region (cut)
C-y		yank (paste)
C-x C-c		save-buffers-kill-emacs (exit, quit)
C-x f		find-file (open file)
C-x C-f		find-file (open file)
C-x C-r		find-file-read-only (open read-only)
C-x s		save-buffer (save)
C-x C-s		save-buffer (save)
C-x C-w		write-file (save-as)
ESC w		append-next-kill (copy)

C-SPC		set-mark-command (mark start of cut/copy)
C-a		beginning-of-line (move cursor home)
C-b		backward-char (move cursor left)
C-d		delete-char (delete character at current position, e.g. delete forward)
C-e		end-of-line (move cursor)
C-f		forward-char (move cursor right)
C-g		keyboard-quit (cancel)
C-h		backward-delete-char (backspace)
C-k		kill-line (delete line)
C-l		recenter (redraw screen)
RET		newline
C-n		next-line (cursor down)
C-o		open-line (insert blank line)
C-p		previous-line (cursor up)
C-q		quoted-insert
C-r		search-backward
C-s		search-forward
M-z		scroll-up (page down. By Emacs tradition C-v, but that mean "paste" to everyone else.)
M-a		page up
C-w		kill-region (cut)
C-x		Control-X-prefix
C-y		yank (paste)
M-n		up-one (Alt-n Emacs Alt is somewhat like a shift/control Meta key.)
M-p		down-one (Alt-p Emacs Alt is somewhat like a shift/control Meta key.)

C-_		undo (control-shift-underscore)

C-x C-b		list-buffers
C-x C-c		save-buffers-kill-emacs (exit, quit)
C-x C-d		list-directory
C-x f		find-file (open file)
C-x C-r		find-file-read-only (open read-only)
C-x s		save-buffer (save)

C-x C-w		write-file (save-as)
C-x C-x		exchange-point-and-mark (move cursor to mark location)

C-x (		start-kbd-macro
C-x )		end-kbd-macro
C-x e		call-last-kbd-macro (run macro)

C-x n		other-window (when screen is split, switch to other window)
C-x 1		delete-other-windows (unsplit window, make one one window)
C-x 2		split-window-vertically (split screen into two windows)

C-x =		what-cursor-position

C-x b		switch-to-buffer
C-x c		compile
C-x C-n		next-error

C-x f		find-file (open file)

C-x k		kill-buffer (close file)

ESC w		append-next-kill (copy)
ESC ESC		repeat-complex-command
ESC g		goto-line

Useful commands:
apropos (search Emacs function and variable descriptions)
manual-entry (open a man page)
toggle-read-only (toggle a buffer between read-only and read-write)
describe-key-briefly (describe a keystroke command)
desecribe-bindings (list all current commands which have keystrokes)