Linux Classes
Share This With a Friend  

Will Emacs Make Me Popular With Women?

The Emacs Editor

The Emacs editor is a lot easier to use than vi. There are no silly modes to trip you up--when you want to enter text, you just position the cursor and type. Gee, what a great idea. It also has built-in help. But while Emacs is a vast improvement over vi, you'll still have to remember quite a few commands to be productive, and you'll probably get a little lost. If vi is a relic of the sixties, Emacs sports the cutting-edge technology of the early eighties. If you get into a situation where Emacs seems to be stuck, or if you don't know what to do, press ctrl-G and things will return to normal. (The ctrl-G key cancels the current operation in Emacs.)

Let's try creating a file from scratch using Emacs. To start, enter this command:

$ emacs bulb.joke

In response, you should see a screen that looks like this:


-----Emacs: bulb.joke (Fundamental)--All--------- ---------------

(New file)

Adding New Text to Your File

Your cursor (the vertical bar) is in the upper-left corner of the screen, and the message at the bottom tells you that a new file called bulb.joke was just created. The body of the file is blank (no tildes as in the vi editor).

To enter new text, just start typing! There's no need to press any special keys first. Go ahead and enter the text shown below: (Press enter at the end of each line.)

Q: How many surrealists does it take to change a light bulb?

A: Two--one to hold the giraffe, and the other to fill the

bathtub with brightly colored machine tools.

--**-Emacs: bulb.joke (Fundamental)--All------------------------

Saving Your Work

To save your work without leaving Emacs, press ctrl-X (for exit) and then ctrl-S (for save). A message at the bottom of the screen should confirm that the file was written to disk. It's a good idea to save your work every few minutes, just so you don't lose it all if the system suddenly crashes.

To save your file and exit Emacs, press ctrl-X and then ctrl-C. A prompt like this should appear at the bottom of the screen:

Save file bulb.joke? (y, n, !, ., q, C-r or C-h)

Type the letter y to save your file and exit the editor. After leaving Emacs, you'll be back at the Linux command prompt. (I'll discuss the other choices later.)

Common Emacs Commands

Have a look at this list of Emacs commands, and then we'll do one more exercise before moving on.

Positioning the Cursor

Move cursor one space right (also ctrl-N).
Move cursor one space left (also ctrl-P).
Move cursor up one line (also ctrl-F).
Move cursor down one line (also ctrl-B).
ctrl-V Move forward one screen (also pgup).
esc-V Move backward one screen (also pgdn).
esc-< Move to beginning of file.
esc-> Move to end of file.
esc-F Move cursor to next word.
esc-B Move cursor back one word.
home Move cursor to beginning of line (also ctrl-A).
end Move cursor to end of line (also ctrl-E).
del Delete character at cursor (also ctrl-D).
ctrl-K Delete to end of line.
ctrl-Y Undelete the last thing you deleted.
ctrl-X U Undo the last command.
ctrl-S Perform a search (forward).
ctrl-R Perform a search (backward).
ctrl-H View online help.
ctrl-H T View Emacs tutorial

Exiting or Saving Your File

ctrl-X ctrl-S Save your file and exit
ctrl-X ctrl-C Quit without saving

Unlike vi commands, Emacs commands are not case sensitive, so whether you enter ctrl-H or ctrl-h, for example, you'll get the same result. There are many other Emacs commands, but the starter set shown here should be enough to get you rolling.

Trying Out Some Emacs Commands

Here's another example to let you try out some of these Emacs commands. Enter the following command to fire up Emacs again, and you should see the file as we left it in the previous example.

emacs bulb.joke

Changing Text and Moving through Your Document

To change text that's already in the file, you first need to reach it. You can move through your file in various ways. The arrow keys provide the simplest way, and they work just like you'd expect them to--they move you character by character through your file.

If you want to jump to the beginning or end of the current line, use the home and end keys, respectively. You can jump from word to word using esc-F and esc-B, or you can scroll page by page using ctrl-V (next page) and esc-V (previous page). On some keyboards, the pgup and pgdn keys work, too.

Text that you enter is inserted to the left of the cursor, and any text to the right of cursor is pushed to the right as you type. You can use the del key (or ctrl-D) to delete one character at a time, or press ctrl-K to delete all characters from the cursor to the end of the line.

Searching for Text

When you're working in a big file, you'll often want to locate a certain string of text. You can either scroll through the file until your eye catches it, or you can use ctrl-S to search using Emacs' less-than-elegant search tool.

As soon as you press ctrl-S, you can start to enter the text you want to find, but don't type too fast! As you enter your search string, Emacs immediately moves the cursor to a word in the file that matches what you've entered so far, so if you type too fast, you may miss something.

Try it. Starting at the top of the file, press ctrl-S and then type the letter b. The cursor should jump to the word bathtub right away. If you now type an r after the b, Emacs searches for br, and the cursor should jump to the word brightly. You can search backward too, by using ctrl-R instead of ctrl-S.

Adding and Deleting Lines

Let's practice adding and deleting lines now. To add a new line to your file, move the cursor to the end of a line (press end or use the right-arrow key); then press enter. A new blank line should appear after the current line. Pressing enter with the cursor at the beginning or end of a line adds a new blank line before the current line; pressing enter with the cursor anywhere else on a line splits the line at that point.

To delete a line, move to the beginning of the line (with the home key) and press ctrl-K. The line will become blank, but it will not go away. To delete the blank line from the file, press ctrl-K again or press the del key.

Cutting and Pasting with Emacs

If you ever want to move a block of text from one spot in your file to another, you can use ctrl-K (kill) along with ctrl-Y (yank) to get the job done. When you press ctrl-Y, the last thing you "killed" with ctrl-K will be yanked back into the file. So to move a block of text, just kill one or more lines with ctrl-K; then put your cursor at the spot where you'd like to place the text and press ctrl-Y to yank it back.

For example, here's how to move the first line of the bulb.joke file to the end. With your cursor on the first character of the first line, press ctrl-K twice. (Line 1 disappears.) Press the down arrow twice to move your cursor to the third line of the file, which is now blank. Press ctrl-Y once to paste the deleted line at line 3 and then press ctrl-Y again.

You should now have two copies of the deleted line at the end of your file, which should look like this:

A: Two--one to hold the giraffe, and the other to fill the

bathtub with brightly colored machine tools.

Q: How many surrealists does it take to change a light bulb?

Q: How many surrealists does it take to change a light bulb?

--**-Emacs: bulb.joke (Fundamental)--All------------------------

Quitting without Saving Your Changes

To leave Emacs without saving your changes, press ctrl-X and then ctrl-C. A prompt like this will appear at the bottom of the screen:

Save file bulb.joke? (y, n, !, ., q, C-r or C-h)

Press n and then answer yes when Emacs asks:

Modified buffers exist; exit anyway? (yes or no)

Your Linux prompt will return, and the bulb.joke file will be just as it was before you started editing.

Parting Words about Emacs

We haven't even scratched the surface of what you can do with the Emacs editor. As its name suggests, you can write your own macros to add functionality to the editor or to automate repetitive editing tasks.

Take the time to go through the Emacs tutorial (accessed by pressing ctrl-H T) to get an idea of all the things you can do with it. Although it's not easy to learn all the various ctrl and esc commands, if you're a programmer (or even if you dabble), Emacs' customizability makes it your best bet as a text editor.

The Usenet newsgroup comp.emacs is a good place to discuss Emacs or ask questions. You can find the Emacs FAQ file online at

For more information on the emacs command, see the emacs manual.

Previous Lesson: The Vi Editor
Next Lesson: The Pico Editor



Comments - most recent first
(Please feel free to answer questions posted by others!)

Seth     (18 Nov 2011, 22:37)
Its not called Linux Emacs its called GNU Emacs Linux is only a kernel. All the components are from GNU. Linux has nothing to do with Emacs
Majed     (16 Nov 2011, 03:57)
ah sorry it was q to quit the help page :)
Majed     (16 Nov 2011, 03:51)
@sazzy command not found means emacs is not installed, in gentoo i installed it using #emerge emacs, on other distributions i think the yum command is used to install software.
anyway once i press ctrl-H T how to get back to emacs, i tried ctrl-G but it didn't work.
BHUSHAN G. DANGRE     (07 Nov 2011, 22:46)
Linux is made easy by this site to me thanks a lot for that .
S.Sathya     (07 Jan 2011, 00:11)
Please add the command for copy the no:of lines in the emacs editor. And also past command.
sazzy     (29 Oct 2010, 02:32)
Hi. Many thanks for creating this useful and helpful website! easy for beginners like me to understand..

nway, regarding this topic... i can use vi, but not emacs.
error result : -bash: emacs: command not found
Matt     (09 Oct 2010, 22:11)
learn how to program, learn computer science theory, perhaps some linear algebra or advanced math if you're weak, perhaps some computer hardware architecture fundamentals... basically learn enough non-trivial stuff about computers so you can recognize flaws to exploit for whatever your purpose may be. To be legit and not some punk who googled a netbios hack but actually know what you're doing, you essentially need knowledge on the order of a CS major and similar and then make sure you have no social life
Charles     (06 Aug 2010, 20:41)
Hackers don't teach how to hack. You have to learn how to hack. If you try to contact most hackers they will crash your machine to get you to go away....
oshane chambers     (14 Apr 2010, 16:41)
How can I become a hacker?

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Notify me about new comments on this page
Hide my email

Ask Bob Rankin - Free Tech Support

Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.