New HOWTO: Emacs Beginner's HOWTO - page 2
Table of Contents, Section 1
2. Running Emacs
2.1. Starting & Quitting Emacs
As a new user, you'll probably want to launch Emacs just to mess
around and try it out. Once you're into Emacs and want to exit,
however, you may not be able to figure out what to do. So if you've
never used Emacs before, give it a shot right now. At your shell
prompt, type emacs and hit enter. Emacs should start up. If not, it is
either not installed or not in your path.
Once you've seen Emacs, you need to know how to exit. The keystrokes
for leaving Emacs are C-x C-c. The C-x notation means hold down the
Ctrl key and press x. In this case, you'll then need to hold down Ctrl
and press c to finish the task.
The keystrokes used in Emacs will likely seem odd, foreign, and maybe
even uncomfortable to you at first--especially if you're a vi user.
Unlike vi, Emacs doesn't have separate modes for editing text and
issuing commands.
To re-cap: emacs will start Emacs. C-x C-c will exit Emacs.
2.1.1. What you'll see
When Emacs starts up it will consume a whole X window (or screen if
you're running on a console instead of in the X Window System). You'll
see a menu across the top, some text in the main part of the screen,
and a couple of lines at the bottom.
It will look something like this ASCII sketch:
+----------------------------------------------------------------------+
|Buffers Files Tools Edit Search Mule Help |
| |
|Welcome to GNU Emacs, one component of a Linux-based GNU system. |
| |
| |
| |
| ... |
| |
|---1:---F1 *scratch* (Lisp Interaction)--L1--All-------------|
|For information about the GNU Project and its goals, type C-h C-p. |
+----------------------------------------------------------------------+
NOTE: Emacs will usually fill the entire screen/window. I've shrunk
the above example to save space here. You will also see a welcome
message in Emacs when you first start it. I omitted that as well and
substituted ``...'' instead. The welcome message simply identifies the
exact version of Emacs you are using as well as pointing you to the
on-line help and related items.
2.1.1.1. The Menu Bar
The topmost line of the Emacs interface is a menu. If you're running
X, you'll recognize them as traditional pull-down menus that you can
access using your mouse. Otherwise you'll need to use keyboard
shortcuts (not covered here) for accessing the menus.
2.1.1.2. The Status Bar and Mini-buffer
Of the last two lines in the Emacs interface, the topmost one is
essentially a status bar. It contains information about the buffer
you're working in, which mode Emacs is in, and various other things.
For now, just realize that it's there.
The bottommost line is called the mini-buffer. It is separated from
the main buffer by the status bar we just discussed. You can think of
the mini-buffer as the Emacs ``command-line''. It is where commands
that you give Emacs appear and it is where status messages are printed
in response to things you do.
You'll find that what I've called the status bar is usually referred
to as the mode line in Emacs related documentation. It is where Emacs
displays information about the current modes(s) you may be using as
well as things like the current date and time, line number, file size,
and almost anything else you might want to see there.
2.2. Some Terminology
This section covers the most basic of Emacs terminology that you'll
encounter when using and reading about Emacs.
2.2.1. Buffers & Files
Unlike some editors, when you open a file in Emacs it does not stay
``open'' the entire time you're working with it. Instead, Emacs reads
the file into a buffer in memory. While you're editing the buffer and
working with the data nothing is changed on disk. Only when you
actually save the buffer does the file on disk get updated. There are
advantages and disadvantages to this approach but it is only important
that you understand that it works this way.
As a consequence, you will see the term ``buffer'' used in Emacs
documentation, modes, packages, and so on. Just realize that buffer
means ``a copy of the file that is currently in memory.'' Oh, it's
worth pointing out that a buffer doesn't always have to refer to a
specific file on disk. Often times Emacs will create buffers as the
result of commands you run. Such buffers may contain the result of the
command, a list of selections to pick from, and so on.
2.2.2. Point & Region
In Emacs lingo, you'll often hear or see references to the point. In
general terms the point is the cursor. The actual distinction between
the point and cursor probably isn't important when you're first
starting out with Emacs. But if you are curious, think about it this
way. The cursor is the visual representation of the point. The cursor
is always ``on'' a particular character position in the current
buffer. The point, on the other hand, lives in the space between
characters on in the buffer. So you might say that if the cursor is on
the letter `h' in the word ``the'' then the point is between the `t'
and the `h'.
Like many modern editors, Emacs allows to perform operations (indent,
spell-check, reformat, cut, copy, paste, ...) on a section of the
current buffer. You can highlight (or ``mark'') a block of text using
the keyboard or mouse and then perform operations on just the selected
block of text. In Emacs, that block of text is called a region.
2.2.3. Windows
Okay, this will be a bit confusing to anyone who has ever used a GUI
interface before. Just remember that Emacs was developed long before
GUI interfaces and window managers were popular.
A window in Emacs is an area of the screen in which a buffer is
displayed. When Emacs is first started, you have one window on your
screen. Some Emacs functions (such as the help and documentation)
often [temporarily] open up additional windows in your Emacs screen.
Emacs windows have nothing to do with X windows in the GUI sense. You
can open up additional X windows to display Emacs buffers, maybe to
compare two files side by side. Those new X windows are referred to as
frames in Emacs lingo. Read on.
2.2.4. Frames
In Emacs, a frame is a separate X window in which an Emacs buffer is
displayed. But both are part of the same Emacs session. The behavior
is somewhat (but not too much) like what happens if you hit Alt+N in
Netscape Navigator.
2.3. Keyboard Basics
This section covers the basics of keyboarding for Emacs. Like every
powerful editor, everything that you can do with Emacs is just a few
keystrokes away.
If you're a vi user, the notion of using the k, j, l, h keys to move
up a line, down a line, forward by a character, and backward by a
character probably took some getting used to. In fact, it might have
taken you a few hours or even weeks of practice before you could
comfortably navigate a file using the various key combinations
available in vi.
Emacs is no different. There are different keystrokes and commands to
learn. Just like vi, you only need to master the basics to get a lot
of work done. Then, as time goes on, you can slowly expand your
knowledge and find faster ways of doing things.
2.3.1. Command Keys (Meta, Esc, Control, and Alt)
As you'll soon learn, Emacs makes heavy use of multi-key combinations.
Because it is not a modal editor like vi, you don't have to think
about being in ``command mode'' or ``editing mode'' before you can try
to move the cursor or execute a command. Instead, you just press the
right combination of keys and Emacs does as told (usually).
The keys that Emacs makes the most use of are usually abbreviated in
the documentation as C (for Control or Ctrl) and M for (Meta). While
most modern PC keyboards have one or more keys labeled Ctrl few have
one labeled Meta. You'll want to mentally substitute either Esc or Alt
for the Meta key. In most standard configurations, both Esc and Alt do
essentially the same thing.
So when you see a reference in any Emacs related documentation to C-x
f it means ``press control-x and then f.'' And if you see a reference
to something like M-x shell is means ``press alt-x and type the word
shell''.
A very useful command for beginners is M-x apropos or C-h a. apropos
will search the Emacs on-line documentation for all functions and
search for the regular expression you type. This is a great way to
discover all commands related to frames. Simply C-h a and then frame.
2.3.2. Moving Around in a Buffer
Now that you know what all those fancy abbreviations mean, here's a
list of the most common keystrokes for moving within a buffer:
Keystrokes Action
-----------------------------------
C-p Up one line
C-n Down one line
C-f Forward one character
C-b Backward one character
C-a Beginning of line
C-e End of line
C-v Down one page
M-v Up one page
M-f Forward one word
M-b Backward one word
M-< Beginning of buffer
M-> End of buffer
C-g Quit current operation
-----------------------------------
And, as you might expect, the cursor keys (or arrow keys) usually work
just as you'd expect. Your Backspace may not. That's another story.
:-(
2.3.3. Essential Commands
Okay, now that you know how to move around within a buffer what about
opening and saving files? Search? Here are some basic commands.
Before we jump straight to those commands, I need to briefly point out
how this works.
All ``command keystrokes'' in Emacs (those that are M-x something or
C-something) are actually just shortcuts to functions which are part
of Emacs. You can call any of those functions by typing M-x function-
name and hitting Enter. You can also use the keyboard shortcut for
that function (if it has one).
For example, the Emacs function which saves a buffer to disk is called
save-buffer. By default it is also bound to C-x C-s. So, you can
either use they shortcut to save the current buffer, or you could type
M-x save-buffer and achieve the exact same result.
All of the most common functions have keyboard shortcuts by default.
Some of them are listed below.
Keystrokes Function Description
-------------------------------------------------------------------
C-x C-s save-buffer Save the current buffer to disk
C-x u undo Undo the last operation
C-x C-f find-file Open a file from disk
C-s isearch-forward Search forward for a string
C-r isearch-backward Search backward for a string
replace-string Search & replace for a string
replace-regexp Search & replace using regexp
C-h t help-with-tutorial Use the interactive tutorial
C-h f describe-function Display help for a function
C-h v describe-variable Display help for a variable
C-h x describe-key Display what a key sequence does
C-h a apropos Search help for string/regexp
C-h F view-emacs-FAQ Display the Emacs FAQ
C-h i info Read the Emacs documentation
C-x r m bookmark-set Set a bookmark. Useful in searches
C-x r b bookmark-jump Jump to a bookmark.
-------------------------------------------------------------------
As you try many of those functions, you'll notice that many will
prompt you for input. They will always to do in the mini-buffer. This
is similar to using the : commands in vi or most commands that you'd
use within your favorite Unix shell.
Emacs has literally hundreds of built-in functions available. The list
above is a tiny sample that represents those that I use regularly. See
the on-line help for a more complete listing of the available
functions and more complete documentation on those I mentioned above.
2.3.4. Tab Completion
Like many popular Unix shells (bash, csh, tcsh, ...) Emacs offers
command completion via the Tab key. In fact, the command completion in
bash was modeled after that in Emacs, so if you use that feature in
bash you'll be right at home.
As an example, try M-x search and then hit Tab. Emacs will append a
hyphen to indicate that there are several possible completions but
they all have a hyphen as the next character. Hit Tab once more and
Emacs will display a list of the possible matches for you to choose
from. Notice that it does so in a new window. It temporarily splits
your display into two windows: one which contains the buffer you were
editing and the other contains the list of possible completions for
``search-''. You may hit C-g to exit out of the selection process and
close the new window.
2.4. Tutorial, Help, & Info
Emacs comes with an on-line tutorial which walks you through the basic
editing features and functions that everyone should know. It also
explains how to use the other help features in Emacs.
I highly recommend that you spend some time going through the tutorial
if you plan on making a serious effort to learn Emacs. As shown in the
table above, you can enter the tutorial via C-h t. The tutorial is
self-guided and aimed at folks who are just getting started with
Emacs.
If you are running Emacs in X, you will see that the rightmost menu on
the menu bar is labeled Help. As you explore the Help menu notice that
some items have keyboard shortcuts and those are listed right in the
menu.
Finally, to see the volume of documentation available with Emacs, you
should try M-x info or C-h i which launches Info, the Emacs
documentation browser.