Some motivation (hopefully) to learn emacs

Those of you who are regular readers of this blog will know that I’ve recently begun trying to learn how to use the emacs editor, and have been blogging a set of emacs tutorials based on what I’ve learned so far.  There’s nothing quite like a teacher that’s only a couple chapters ahead of the students right? 🙂

One thing I realize I haven’t communicated particularly well is *why* I’ve decided to learn emacs, much less why *you* should learn emacs.  So today, I’m going to post links to a few of the articles and screencasts that helped to inspire me to learn emacs.

First up is an awesome screencast from  Marshall Vandegrift where he quite ably demonstrates how slick a fully customized and turbo’ed out emacs can be.   His screencast demonstrates emacs as a Ruby on Rails development environment, with specific comparisons to featurs of Textmate, but most of the features he demonstrates are available in any language supported by emacs.

Another good Rails on emacs screencast is this one from the author of one of a very cool Rails minor mode for emacs.

Here’s a third emacs screencast.  This one from Derek Slager demonstrates the how to reformat a really ugly CSS file using some basic emacs features, mostly regex’s and macros.

Derek also has a great post called “The Case for Emacs” where he explains his reasons for preferring emacs as an editor/IDE.  One of Derek’s major points, that with emacs you can use the same editor and the same bag of tricks for any language (java, C#, Ruby, Erlang, etc) is definitely high on my list of reasons for learning emacs.  I like experimenting with new languages, but switching IDE’s is a pain, and once you leave the mainstream languages and start playing with languages like Erlang, Haskell, Lisp, etc. the situation is even bleaker.  Unless you use emacs. 🙂

Last but not least is the always enlightening, entertaining, and certainly verbose Steve Yegge.  Steve’s post on Effective Emacs was probably the final catalyst that really got me off my butt and made me decide to learn emacs.  I found items #4 and #6 on Steve’s list to be particularly helpful in understanding why the primitive looking emacs might hold some productivity secrets that newer IDE’s still haven’t caught up with.

Well, there you go. Five resources to (hopefully) motivate you to learn emacs.


If you’re following along with my Emacs tutorials (or even if you aren’t) and you’re looking for a good way to practice your emacs keybindings, you might want to try XKeymacs. It’s an interesting little app that’ll enable emacs keybindings pretty much everywhere in Windows, or just in specific Windows programs if that’s what you’d prefer. But it’s not just limited to providing emacs keybinding in Windows. It’s also got surprisingly flexible capabilities for doing just about any kind of keyboard remapping you can think of. I highly recommend it.

Learning emacs part 4: buffers, windows, and frames

So far, we’ve learned how to open files, save files, and navigate around a file. But it’s pretty lame to only be able to work on one file at a time. So let’s look at how to work with multiple files in emacs.

Recall that emacs was created waaaay back in the almost prehistoric times before modern windowing interfaces. Why is this important? We’ll see shortly.

But first, lets talk about buffers. In emacs, each file you open is contained in a separate buffer. However, not every buffer contains a file. You can also have an unlimited number of scratch buffers, which are buffers that haven’t been saved to a file. Additionally, there are other specialized buffers, like the minibuffer, buffers that contain the emacs online help system, buffers that are attached to the input/output of a shell or other process, etc.

Buffers are displayed in windows and frames. And here is where the antique nature of emacs becomes important. Remember that emacs originally didn’t run in a windowing GUI. Instead, it ran full screen in a character mode terminal. So in emacs terminology opening a new window didn’t mean creating an overlapping, independent window like you’d expect in a modern GUI. Instead, it meant splitting the screen horizontally and/or vertically two or more times. Within each “window” you can either display separate buffers, or you can display multiple views into the same buffer. What everyone else in the world calls windows are called “frames” by emacs.

So now that we know what buffers, windows, and frames are (in emacs terminology at least), how do we use them? To begin, lets open the alice1.txt file that we worked with in the last tutorial. If you don’t still have the file saved somewhere, go ahead and download it from the link above. Open it in emacs (C-x C-f). Congratulations! you’ve just created a buffer. In a moment we’ll look at how to create more buffers, but first let’s look at how to work with windows.

Emacs Windows

It’s important to learn how to work with emacs windows, as many emacs command will open windows to display their output or to accept input or interaction beyond what can be done in the minibuffer.

The simplest way to create new emacs windows is to split the current window in half, either horizontally or vertically. The commands to do so are:

C-x 2: Split vertically
C-x 3: Split horizontally

When you split the current windows into two windows with C-x 2 or C-x 3, you see that each of the new windows contains a view into the same buffer (in this case, the buffer containing the alice1.txt file). You can scroll around in each of the windows independently, but because they are two windows into the same buffer and not two buffers, any change you make to the text in one window will also affect the text seen in the other windows. Go ahead and try it. Scroll the buffer in each of the two windows so that you’re looking at the same line of text. Then start typing. You’ll see the characters you type “echoed” in the opposite window. Pretty cool huh?

Now that you have two (or more) windows open, how do you switch between them? The command C-x o (that’s a lower case O, not a zero) moves the cursor to the Other window. Using C-x o repeatedly will cycle the cursor through all of the current windows.

To close the current window, type C-x 0 (zero this time, not O). To close all windows except the current one, type C-x 1.

Emacs Buffers

Of course opening multiple windows into the same buffer is only of limited utility. I’d be rather disappointed if that’s all we could do, but like most editors, emacs lets you do a lot more than that. You can open a new file, thereby creating new buffers, in either window, and you can switch which buffer is displayed in your current window.

To list what buffers are available, use the list buffers command, C-x C-b. This command will split open a new window. In that window is displayed the list of currently open buffers. It also displays some additional information about each buffer as well. The buffer list displays 6 columns. From left to right they are:

Column Contents
M An asterisk (*) is displayed in this column if the buffer has been modified since it was last saved
R A percent sign (%) is displayed in this column if the buffer is read-only
Buffer The name of the buffer
Size Size of the buffer in Bytes
Mode The Major mode active in the buffer. We haven’t discussed modes yet, so don’t worry if this column doesn’t make any sense just yet. It will soon. 🙂
File The name of the file, if any, load into the buffer

There are two ways you can switch to a different buffer. While the buffer list is displayed, you can move the cursor to the line of the buffer you want to switch to, then press enter. That will switch the buffer displayed in the window that the buffer list was previously displayed in. If you want to get rid of all other windows, hit C-x 1.

The other, quicker way to switch buffers is with the command C-x b. This command will prompt you in the minibuffer for the name of the buffer you want to switch to. You can type the complete buffer name, or you can type part of it and use the standard emacs tab completion.

You can also close a buffer (emacs calls it “killing” the buffer) with the command C-x k. If there are unsaved changes in the buffer you’ll be prompted to save the buffer first. If you want to save any unsaved changes in all open buffers, use the command C-x s.

Notice the similarity between the keystrokes for the switch buffers command (C-x b) and the list buffers commands (C-x C-b). Emacs is known for having a rather sharp learning curve, but these similarities between the default keybinds for related commands does help a little.

Emacs Frames

If you’re running a GUI version of emacs under X-Windows, MSWindows, or OSX, you also have the option of opening a new frame (which you’ll remember, is what’s called a window to anyone outside the emacs community!). The commands for working with frames are very similar to the command for working with windows. To create a new frame, use the command C-x 5 2. To delete the current frame, use C-x 5 0 (that’s zero, not O). To delete all but the current frame, use C-x 5 1.

These are not all of the emacs commands for working with buffers, windows, and frames., but these are the most common and basic ones. We’ll certainly learn about more of the others as we proceed in our shared journey to emacs mastery.

Speaking of our journey, lets review what we’ve learned so far in this series:

Working with files:

Command What it does
C-x C-f Open (Find) a file
C-x C-s Save the current file
C-x C-w Save As
C-x s Save any unsaved files

Cursor movement:

Command What it does
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 (down)
C-p Move the cursor to the previous line (up)
M-f Move the cursor forward 1 word
M-b Move the cursor backward 1 word
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 paragraph
M-e Move the cursor to the end of the paragraph
C-v Move the cursor one page down
M-v Move the cursor one page up
M-< Move the cursor to the beginning of the file
M-> Move the cursor to the end of the file
C-l Recenter the screen around the current line

Working with buffers:

Command What it does
C-x C-b List buffers
C-x b Switch to named buffer (with tab completion)
C-x k Close (kill) the current buffer

Working with windows:

Command What it does
C-x 0 Close current window
C-x 1 Close all windows except the current one
C-x 2 Split current window in two vertically
C-x 3 Split current window in two horizontally
C-x o Switch to other window

Working with frames:

Command What it does
C-x 5 0 Close current frame
C-x 5 1 Close all frames except the current one
C-x 5 2 Create a new frame


Command What it does
C-x C-c Exit Emacs

That’s all for this installment. Join us next time when we’ll explore marking text, working with marked regions, searching, and how to get out of trouble if you accidentally hit the wrong key sequence!

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

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.

Learning Emacs – part 2: the emacs user interface

In part 1 of this series, we looked at installing emacs. This time, lets get acquainted with the emacs user interface.

The emacs user interface

Below is a screenshot of an average emacs screen with several noteworth elements labeled.

Emacs screen

The first two elements of note are the menubar and toolbar. These are pretty much the same as what you’re used to from any other GUI application, but don’t let that fool you into thinking that emacs works like any other GUI application! The first rule for understanding and learning emacs: Emacs is old. It’s been around since the pre-historic days of computing, and learning to use emacs will sometimes feel like a journey back in time. Gnu Emacs comes from a time before graphical user interfaces were common. Mouse support, menus, and limited graphics capabilities have been grafted onto it over the years, but at it’s heart, emacs is still a text mode console application. You have to understand that to ever understand emacs.

Emacs is a relic from another era. You have to accept or reject it on it’s own terms. It is not, and will probably never be more than superficialy similar to any of the other applications you probably use. In a later installment, I’ll show you how to disable the menubar and toolbar. I know that sounds crazy, but IMHO it’s essential that you force yourself to learn emacs on its own terms.

The next noteworthy UI element is the minibuffer. It’s the seemingly blank line at the very bottom of the emacs window. The minibuffer is where you’ll enter longer commands, and where you’ll enter arguments to commands that require arguments. A simple example would be using the C-x C-f command to open a file. The minibuffer area is where you’ll be prompted to enter a filename. (if you don’t know what C-x C-f means yet, don’t worry. We’ll go over that soon.

The final major UI element we’ll talk about is the modeline. The modeline contains a number of useful pieces of status information, including the name of the current buffer (usually the name of the file being edited in the buffer), the current modes (major mode and minor modes), the current position of the cursor in the buffer, amount of the buffer displayed on screen, etc.

That’s it for this installment. Next time we’ll start getting hands on with some basic commands including cursor movement, insertion and deletion, etc.

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

Learning Emacs – part 1: Introduction, entering emacs, and exiting emacs

So I’ve decided to learn Emacs. I’ve always been more of a Vi man previously, and I still consider some moderate amount of comfort with Vi to be a necessity for any Unix sysadmin. I’ve also used a few of the major IDE’s for programming: I use Eclipse with RadRails for all of my Ruby development, a friend who’s trying to get me to learn Java has introduced me to NetBeans, and I’ve played a bit with C# in Visual Studio.

So why learn Emacs? Well, there are a so many people on the net who swear by Emacs, including some who otherwise seem to be very sensible and non-masochistic. 🙂 It’s also supposed to be the environment of choice for Lisp and Scheme programming, which is something I’d like to learn more about. So, operating under the principle that “where there’s smoke there may be fire”, I’m going to give Emacs a chance and see if I can learn enough of it to at least stand it. 🙂

I’m starting with installing Gnu Emacs under Cygwin. I won’t go into details on how to install Cygwin, as I think that’s covered quite completely elsewhere (specifically in the Cygwin documentation), although I may do a later post on how I’ve customized my Cygwin environment to make it more comfortable and more usable. What am going to do, is document my experiences learning Emacs, and what I’ve learned.

Under Cygwin, emacs is installed in the usr\bin directory under the cygwin directory (on my system “c:\cygwin\usr\bin”).

To start emacs from a shell prompt, just type “emacs” or you can type “emacs filename.txt” to open a file (in this case, called filename.txt). In a later installment of this series, we’ll look at how to open files from within an already started emacs.

To exit emacs use “C-x C-c”, which mean to hold down the “Control” key while hitting the “x” key, then hold down the “Control” key while hitting the “c” key.

Under Cygwin, there’s also a startmenu shortcut conveniently created by the Cygwin setup program. Or if you’re really smart, you can use Launchy (like I do). 🙂

If you’ve got X installed (which you should), you’ll get a graphical emacs like this:

Emacs in X

If you’re not running X, you’ll get a slightly less friendly looking version of Emacs, like this:


There’s also a native Win32 Emacs version.

From what I remember of being a Mac version, you could get Gnu Emacs from Fink, or there are some native Aqua ports of Emacs.

If you’re a Linux user then it should of course be easy to get a copy of Gnu Emacs from your preferred Linux distribution. 🙂

Next time, we’ll talk about actually using the damn thing. 🙂

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