Using Emacs


Staff member
Oct 27, 2011
Reaction score
Editor of Macros

Richard M. Stallman, founder of the Free Software movement and the GNU project began development on Emacs in the 1970's and like Vi, it has become a staple of the Unix/Linux world. Emacs stands for Editor MACroS, which Stallman developed at MIT so he could use the rather difficult TECO editor more easily. Stallman started development on the current incarnation, GNU Emacs, in 1984. This is the cornerstone of applications of the GNU project. After nearly two decades of development, it has more features that you can shake a stick at. It would take an encyclopedia to document all that Emacs can now do. Fellow Emacs users have called it a quasi-operating system. What we're going to do here though is to talk mainly about its text editing features, which should only take up 6 hours of your time! (just kidding)

Emacs for X-window and XEmacs

There is a branch-off of the Emacs project called XEmacs that even includes a web browser. We're not talking about this application. We're going to deal with GNU Emacs, which is available in text mode or in an X-window version. The version for X includes clickable menus and other GUI features, but it is pure GNU Emacs just the same. All the key strokes we will be referring to from now on are as useful in the X-window version as they are necessary if you run it in text-mode.

Introduction to Emacs keystrokes

Like Vi, to take advantage of Emacs, you need to know some keystroke basics. Most Emacs commands are based on a combination of CTR+?, CTR+x+?, CTR+c+?, ESC+? (where ? is some key). There is also the added feature of pressing ALT+x which opens up what's called a mini-buffer. But before we talk about mini-buffers, let's talk about full-fledged buffers.

Emacs opens up files in what is known as a buffer. This comes in handy particularly when you're using Emacs in text mode. These buffers become like quasi-windows. You can open up a new file/buffer by typing: CTRL+x+f. You can switch back and forth by typing CTRL+x+b. The mini-buffer is a small thin buffer where you can carry out Emacs commands. All you have to do is type ALT+x and the cursor will begin to flash in the mini-buffer. You just need to type in one letter and emacs will begin to show you all of the commands that begin with that letter. Let's say you want to check the spelling of the text in the buffer with the program 'ispell'. You would type:

ALT +x +i + TAB

The mini-buffer begins to get a little less mini and you start to see all of the commands that start with the letter 'i'. Of course, you know that ispell starts with 'isp', so if you continue typing those letters, you will get to the ispell commands. Continue to type the letters until you get the ispell command you want, which in our case would be 'ispell-buffer'. Press enter, and Emacs will invoke ispell to check your entire text file.

The mini-buffer is your friend: The commands you type in the mini-buffer don't go away until you close Emacs, so you don't have to keep typing the same commands. Just press the up arrow until you find the command you used before.

Basic editing with Emacs

To open a file in emacs, all you have to do is type:

emacs the_file

If you want to open another file, type:

and start typing away. When you want to save your text, just press:


When you're finished, you can close or "kill" the buffer like so:

You can also do the killing (I know that sound hideous!) and the saving at the same time by pressing:

And you will be prompted to save your file or just quit.

More keystrokes = more power

GNU Emacs is perfectly suited to GUI work and you can click on menu items and options and get the same results. You may, however, find that in connecting to a remote host, X is not available (usually for security reasons) or it is too slow to be practical. Then, obviously, you need to know the different key combinations to get something productive accomplished. Here we'll explain some of the more important key combinations that you can use to get your work done.

Removing things

How do you get rid of a word you don't want? You kill it. To "kill" a word, you would press:


Let's imagine that you've just typed something a bit strange:

"The rain in Bulgaria falls mainly on the ground"

Problem: You don't want that line in the text. Solution? More killing. Place your cursor at the beginning of that line and press:

Voila. The line will disappear.

Locating and Replacing

If you're at the beginning of a text file and you want locate a word or words in a text, all you have to do is press:


This will open up a mini-buffer and you can enter the text you're looking for. Emacs will automatically go looking for it. If you're at the end of a file, then you can do a reverse search by typing:

Once again, type what you're looking for in the mini-buffer

You can also replace words by pressing:

ESC+% (percent)
Emacs will switch you to the mini-buffer and ask you first which word you'd like to replace and then it will ask you for the substitute. One by one it will show the words and ask you for your consent. Just type 'y' to give it. If you want to replace every instance of that word, just type '!' (exclamation) and Emacs will change them all at once.

Un-doing Things

Imagine that you just "killed" a word. If you feel guilty you can bring your word back to life if you want by pressing:

You will undo your last changes. If you keep pressing that, you keep un-doing everything else before that up to the point where you started editing.

There are some alternative keystrokes to do the same things. For example, CTRL+SHIFT+- (dash) works to undo as well. ALT+SHIFT+% (percent) will also allow you to find and replace. Choose the one that's more comfortable or easier to remember.

Emacs Commands - Unleashed Power Turbo Edition

As I alluded to at the beginner of this section, Emacs has so many commands that it would not be practical to talk about them here. However, we can talk about the ones that will make your life with Emacs a lot easier

Changing the case of the text

Imagine that you've typed a word in capitals that really should be in lowercase and vice-versa. All you need to do is find that word and type:

ESC + l

To change to upper case:

ESC + u

To capitalize a word, place the cursor on the first letter and press:

ESC + c

Cutting and Pasting

Cutting and pasting is not just for those super-duper word processors that cost a fortune. You can do cutting and pasting just fine with Emacs.

First, to select a word, cut and paste it, you would do the following:

To select a word, you would do this:

ESC - @

Now to cut the word, do this:

CTRL + w

Now, to paste the word, do this:

CTRL + y
You can also do this with sentences, paragraphs and the entire buffer. You only have to change the first step above. The second and the third remain the same.

To select a sentence or a phrase, do the following:

and move the right arrow and select the amount of text you want.

Selecting a paragraph is a little easier:

CTRL + h
To select the whole text buffer, is similar:

CTRL + x + h

Again, as I mentioned. The commands for cutting and pasting are the same. But now, let's imagine that you don't want to cut. You just want to copy and paste. To copy a word, sentence, paragraph, etc. you would just press the following keys:

ESC + w
Then you can take it to a different buffer or open a new one and paste the text with CTRL + y

Checking your spelling

With Emacs, you can easily check your spelling. To check the spelling of a word, just place the cursor on the word and press:

ALT + SHIFT + $ (dollar sign)
Navigating the bufferYou can easily go from the beginning to your file to the end with keystrokes - no need to be wearing out the arrow buttons!

ESC + <
will get you to the beginning of the file

ESC + >
will bring you to the end.

To navigate between paragraphs, use the combination of

ESC + }
to get to the beginning of the paragraph and :

ESC + {
to go to the end of the paragraph.

Splitting windows

In the pre-GUI days, this feature must have been really appreciated. Today, we take for granted that you can have a lot of different application windows open, this features is still appreciated, especially when you have to compare parts of the same file. To split a window in two, vertically, just press:

CTRL + x + 2

This comes in handy when you've got to look at two sections of the file at once. To return to normal mode, press:

CTRL + x + 1
You can even split the buffer again and have three, four or more sections. Just press CTRL + x + 2 in each section.

You can also split the window horizontally with the keys:

CTRL + x + 3
CTRL + x + 1 is also used to get Emacs back to normal mode.

Emacs tweaking

If you're not happy with what Emacs does by default, you can modify a file in your home directory and change Emacs' behavior to your heart's content.

In your home directory, among your many "dot" files, you'll find a file called .emacs.

This file is written in Emacs Lisp, which is a variant of the programming language Lisp. So not only will you be tweaking your .emacs file, you'll be learning a little Lisp programming in the process.

There are those, like Paul Graham, who think that Lisp is one of the better programming languages out there. Graham, who is best known for developing Bayesian filtering of spam, developed the Yahoo Store system using Lisp. On the other hand, there are people who find the parenthesis dominated syntax too much to take. Larry Wall, creator of Perl, once compared programming in lisp to eating cereal with toenails mixed into it. Whatever your preferences may be, what I think is important is that we have this wonderful variety of tools for Linux at our disposal.

A simple tweak

One of first changes most users make to their .emacs file is to add a line that removes the default text that appears when Emacs starts. If you'd like to do this as well, just open up your .emacs (in Emacs itself, of course!) and add this line:

(setq inhibit-startup-message t)

If you want to make some comments about what you've just done, you would place a semi-colon (;) before them. That is what differentiates comments from code in Emacs Lisp. So you might write something like this in the file.

;; don't show startup message
(setq inhibit-startup-message t)

Another little thing I find helpful is this tweak:

; Make all "yes or no" prompts show "y or n" instead
(fset 'yes-or-no-p 'y-or-n-p)

That just saves me from having to write the whole word 'yes' (which is Emacs' default behavior) when Emacs asks me if I want to quit without saving, etc. Now I only have to type the letters 'y' or 'n'. Not that it's particularly important in the royal scheme of things, but added up, that 2 stroke savings may make your keyboard last 3.76 days longer.

Keyboard shortcuts

Speaking of keyboards, Emacs has some nice built-in keyboard shortcuts, but there is such a huge number of things that Emacs does, it's impossible to have all of these features bound to keyboard shortcuts. So if you're favorite feature is missing a shortcut, not to worry. You can create one by modifying your .emacs file.

If you do a lot of programming, one of the things you may want to add is a keyboard shortcut to go to line number X of your program. As this doesn't exist by default, you'd add it to your .emacs like so:

;; make CTRL+x+g goto-line(global-set-key "C-xg" 'goto-line)

As you'll remember, keyboard shortcuts are based on the CTRL, ALT and ESC keys, so when you create keyboard shortcuts, you need to use characters that Emacs Lisp understands for those keys. They are:

CTRL = \C-
ESC = \e-
ALT = \M-
If you used Emacs for writing email, you could create a short cut for composing an email:

;; open mail in other 'buffer'
(global-set-key "M-m" 'mail-other-window)

There's really no limit to what you can do to to tweak your Emacs behavior. Emacs comes with all kinds of help files, tutorials and even games if you get bored. It should be of incalculable help in your system administration tasks.

Members online