Learning Emacs – part 3: The basics

In the last two installments of this series, we looked briefly at installing Emacs and at the major elements of the Emacs user interface. In this installment, we’ll look at some of the basics: opening files, saving files, closing files, and basic cursor movement. Excited? So am I. So lets get started.

But first, lets look at a few common Emacs user interface conventions. Most commands in Emacs are executed by holding down either the Control key (labeled Ctrl on most keyboards) or the Alt key (called the Meta key in Emacs) while pressing another key or sequence of keys. In the Emacs documentation, the Control key is denoted by a capital C, the Meta/Alt key by a capital M.

So for example:

C-f means hold the Control key down while pressing f.
M-f means hold the Alt key down while pressing f

It’s more fun to play with some actual text rather than a blank buffer, so thanks to Project Gutenberg, here is the first chapter of Alice in Wonderland. Save it in your home directory, or another convenient folder and we’ll use it in the exercises that follow.

Begin by starting up Emacs. Under most GUI environments there should be an icon. From a Unix or Cygwin prompt just type “emacs”. Once Emacs is running, we will open the alice1.txt file by type C-x C-f (remember, that means hold down the Ctrl key while pressing “x”, then hold down Ctrl with pressing “f”). This should bring up the “Find file” prompt in the minibuffer as shown below:

finding a file

You can either type in the full file name (alice1.txt), or if you can hit tab twice in succession and emacs will list all the files in the current directory. You can also type in part of the filename before you hit tab twice, and emacs will display the files in the current directory that match what you’ve already type in. Try it now. After you’ve type in or tab completed the filename, press Enter and emacs will load the selected file and display it in the current window.

Congratulations! You’ve opened a file in emacs. Now what? Let’s try learning to move the cursor. Yes, you *can* use the arrow keys or, if you’re using a GUI version of emacs, the mouse. But according to the emacs gurus the true path to emacs enlightenment and productivity is to learn the conventional emacs key bindings. I haven’t used emacs enough to know if that’s correct, but I figure if we’re going to spend the time learning emacs, we might as well go all the way. 🙂 So here are a few basic cursor movement commands to try out:

C-f : move the cursor forward 1 character
C-b : move the cursor back 1 character
C-n : move the cursor to the next line
C-p : move the cursor to the previous line

Go ahead and practice those a bit. Move around through the sample file. It’s O.K. I’ll wait.

Done? Ok, lets keep going.

Moving about our file one character or line at a time using control keys combinations is…. a little bit slow and awkward. Fortunately, we can do better. How about moving forward and backward a whole word at a time? Easy enough. Instead of moving forward and backward one character at a time with C-f and C-b, we’ll move forward and backward one *word* at a time as follows:

M-f : move the cursor forward 1 word
M-b : move the cursor back 1 word

That’s a lot better than going one character at a time right? Notice the relationship between the Control and Meta commands. C-f is forward one character, M-f is forward one word. You’ll see that kind of relationship a lot in emacs command. Here’s a few more useful ones:

C-a : move the cursor to the beginning of the line
C-e : move the cursor to the end of the line
M-a : move the cursor to the beginning of the sentence
M-e : move the cursor to the end of the sentence
C-v : move the cursor one page down
M-v: move the cursor one page up

Pretty cool huh? Notice again the relationship between the Control key and Meta key versions of a command. Here’s a couple of slightly trickier ones:

M-< : move the cursor to the beginning of the file
M-> : move the cursor to the end of the file

These two are a little different because you’ve got to hold down the shift key to hit “<” or “>”. So you’re actually holding down Alt-Shift-, to go to the beginning of the file.

There’s also this rather odd one:

C-l : recenter the screen around the current line

It sounds like an odd thing to do, but when you think about it you probably do this with the mouse all the time. You move the scrollbar until the interesting text is in the middle of the window. Emacs just gives you a little shortcut to do exactly that without moving your hands from the keyboard.

Well, that’s about it for basic cursor movement. Before we go, lets look at how to save our file, how to save it with a different file name, and of course, how to exit emacs.

C-x C-s : Save the current file
C-x C-w : Write the file to a different name (this is the same as Save As in most modern programs.
C-x C-c : Exit emacs.

I’ve you’ve got any unsaved changes when you exit, emacs will prompt you in the minibuffer in you want to save those changes.

Well, that’s it for now. I’ll try to have the next installment up in a couple of days. If you like this series, please let me know in the comments.

______
Other installment in this series: part one, part two, part three, and part four.

Advertisements

2 thoughts on “Learning Emacs – part 3: The basics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s