One of the more frequently cited reasons for not learning emacs is, and I quote:
The people who say this are either going about learning emacs the wrong way, or are just making an excuse. There is no reason to sit down and try to memorize a slew of keystrokes. Most people learn better by taking a small bit at a time and working from there. Thus, it's in your interest to know a few keystrokes initially, then as you get used to emacs, you'll pick up others. Before you realize it, you'll know 20 or so of the more valuable keystrokes and that's pretty much all you need. When you want information on a more advanced topic, you can always look it up in the documentation.
Just to give you an idea of what emacs can do for you, here's what I use emacs for:
Ok, so where to start? You need to know some basic emacs notation.
A quick side note before we get into that. I'll put all the items
you type in "code" type face, like this: C-x. Hopefully,
that will help you differentiate between what is the command and what
isn't. Now then, the
keystrokes in emacs are done via holding the control key or by
pressing the `meta' key. The symbol C-x
means to hold down control
and press x. M-x
means to press the `meta' key (Escape on PC and
SUN keyboards), let it go and then press x. The control keystrokes
are just bound to run some particular command that can also be
run by a M-x command. However, the reverse is not
true; there are some M-x commands that have no control
key shortcuts. You can get around this by binding particular keystrokes
to run the commands, but that's an advanced topic that I won't go into
here.
As an example, the keystroke to quit emacs is
, which just happens to be the same as running
.
As you can see, the M-x commands can get a bit long, hence the reason
for having the keystrokes in the first place. Things could get
ugly though for the M-x commands that have no
keystroke. Thankfully,
emacs provides tab completion. Try typing
and then hit the
TAB key. Emacs will try to complete the command as best it can. If
there are more completions, then hitting TAB twice will show them.
(By the way, this is the same mechanism that the bash shell uses for
file name completion. It's not a coincidence.)
One more note. Emacs edits `buffers'. This may seem like a pedantic point, but it's used everywhere in the documentation. When you want to edit a file, you first load it into a buffer, make changes, and then save the buffer. Now, don't be fooled. This is no different than what you do when you use any other editor. The reason emacs makes the distinction is that there is no particular reason to assume that a buffer is always attached to a disk file. Just look at the list above of the things I use emacs for. You can run a child process, such as a shell, debugger, or lisp interpreter, in another buffer. These things are obviously not attached to a particular disk file. Thus, it's important to realize emacs is manipulating buffers that may or may not correspond to a disk file.
Enough background. Here's some keystrokes for you to try:
C-x C-c --
exit emacs. This will ask you to save any buffers
you have changed.
C-x C-s -- save file.
This saves the current buffer.
C-x C-f -- find a file.
This is how you can load a file into buffer.
C-x b -- change buffers.
M-x kill-this-buffer --
removes the current buffer from emacs. (I'd
recommend binding this to something useful like
C-c k , but that's
a more advanced topic).
Did they work? Great!
Now, lemme tell you about a common mistake people first using emacs
make. They treat emacs as if it can only edit one file at a time.
Thus, they get in, make some changes, exit emacs. They then
decide they have to edit another file. So, start up emacs again and
edit that file. Quit and edit another file, etc. This is extremely
absurd. Emacs is an extremely large editor and it does not start
up instantaneously. Once running, it runs quickly, but startup is
kinda slow. Thus, it's to your advantage to learn how to change between
buffers. Personally, I log in, start emacs, and just leave it run
until I log out. When I want to change something in a file,
I use to
find the file, edit it, and save it. If I want to come back to that
file, I'll leave it in emacs and use
to change back to it later.
Alright, so there's some basic stuff. What about programming? Emacs handles this by various `major modes' that give information about the particular programming language (actually, major modes are present for just about every type of file, not just programming files). For example, the c mode tells emacs information about how to indent a switch statement. Emacs tries to guess what major mode to use by the file extension. If it guess wrong, or if the file you are editing has a wrong extension on it, then you can change the major mode to whatever you want. Here are some commands:
M-x c-mode
M-x c++-mode
M-x java-mode
M-x perl-mode
M-x tex-mode
(actually, if you're going to edit LaTeX files, I'd
recommend getting the AUCTeX package, which makes such editing alot
easier. More on this later.)
M-x compile --
asks you for a command to run to compile whatever
it is you're working on (typically `make'). As the command is run,
you'll see any error messages your program generates in a separate
buffer. (Actually, emacs splits the screen so that you see
two buffers, one with your program, one with the compiler error
messages.)
C-x 1 --
Make one screen from a split screen (useful if you've
done M-x compile )
M-x next-error
-- this is how you can parse through the error
meessages. This command will take you to the spot of the next
error in the list from the compiler so you can fix it.
M-x compile
and M-x next-error do not have any keyboard
shortcuts. You can make your own if you wish (I have them bound
to C-c c and
C-c n respectively).
What about copying (or cutting) and pasting? This can get a little complicated, so I refer you to the documentation for a more in depth discussion. What you do is cut/copy the area between `mark' and `point'. Point is the spot just before the cursor. Mark is a spot in the buffer that you define. These two spots define a region that you can cut or copy. You typically cut one thing at a time (though you can get around this). You can copy more than one thing at a time (this ain't windows). Emacs maintains a series of `registers' that you can use to save arbitrary amounts of text. After cutting or copying, move the cursor to where you want the text and paste it in. Here's the commands:
C-spacebar (or
M-x set-mark-command )
-- sets mark to the current
value of point (just before the cursor).
C-w (or
M-x kill-region ) -- cuts all the
text between mark and
point.
C-y
(or M-x yank ) --
inserts the most recently killed text.
C-x r s 1 (or
M-x copy-to-register ) -- copies all the text
between mark and point into register 1. Change the 1 to another
number to use the other registers.
C-x r i 1
(or M-x insert-register )
-- inserts the text in register
1 into the current buffer.
C-_ (or M-x undo )
-- undoes any recent changes (cut text, inserted
text, etc). Emacs has unlimited undo so you can actually undo
all the changes you've made to a buffer since you've read it in.
C-n -- move to the next line.
C-p -- move up a line
C-f -- move forward one character
C-b -- move back one character
C-a -- move to the beginning of the line.
C-e -- move the end of the line.
C-d -- delete character under the cursor.
C-v -- move down a page.
M-v -- move up a page
M-x goto-line --
go to a particular line in the buffer.
C-LEFT -- (LEFT=left arrow) moves back
one word
C-RIGHT -- moves forward one word
C-UP -- moves back to start of paragraph
C-DOWN -- moves forward to end
of paragraph
Well, that covers some basic stuff. For more information on emacs,
see the tutorial. Type
to start up the tutorial within emacs.
It will go over alot of the basic stuff I've covered here. When you're
done with that, you may want to check out the
Emacs for Beginners Howto at
http://www.tldp.org/HOWTO/Emacs-Beginner-HOWTO.html. It'll cover
a few more things. Finally, when you're done with all that, it's
time to check out the online documentation. The authors of emacs
have written extensive documentation that you can buy at a bookstore
like Borders, or you can just read from within emacs. To do so, you
need to use the info mode. Type
to get into it and then
select the Emacs option. Start reading. There's a nice index
that you can use to find a particular item.
Finally, here's some links for a few things you find useful.