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 ftp://rtfm.mit.edu/pub/usenet/comp.emacs.
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
|
|
||