-->
Previous Table of Contents Next


A Summary of Essential Commands

Table 16.1 is a summary of the more essential commands described in this chapter. You should consult the vi man page for more details on the many other vi commands.

Table 16.1. Essential vi commands.

Command What it does

i Starts inserting text at the cursor
h Moves the cursor one character to the left
j Moves the cursor down one line
k Moves the cursor up one line
l Moves the cursor one character to the right
Ctrl+f Scrolls forward one screen
Ctrl+b Scrolls backward one screen
ndd Deletes the next n lines
nyy Yanks the next n lines into the unnamed buffer
p Puts the contents of the unnamed buffer to the right of the cursor
u Undoes the last change
:wq Writes changes and exits vi
:q! Exits vi without saving changes
:set all Shows all set parameters and their values
/string Searches forward for string

The emacs Editor

emacs has become the editor of choice for many users because of its online help facility and its extensive collection of editing commands. For programmers, emacs is especially attractive because it can be configured to format source code for a variety of languages such as C, C++, and Lisp. emacs is somewhat easier to learn than vi, but it also features a much larger set of commands.

Starting emacs

emacs is invoked from the command line by entering


emacs

To start emacs with a file to be edited, enter


emacs filename

If you start emacs with a file, the screen displays the contents starting from the first line. Note the two lines at the bottom of the screen. The first of these lines, known as the mode line, displays the name of the file being edited and the part of the file that you are looking at (for example, TOP,20%,BOT). The last line on the screen is the echo line, which emacs uses to display system messages and as a prompt for more input.

Control and Meta Keys

You are quite free at this point to start entering text into the edit buffer at the cursor location. However, you’re probably wondering, “How do I move the cursor around?” Before explaining this little detail, there are two keys that you should know about: the Control key and the Meta key. The Control key is used in most of the commands for emacs, but some use the Meta key instead. Commands in emacs consist of combinations of the Control or Meta key followed by some other character. It is necessary to hold the Control key when pressing the next character, whereas the Meta key can be pressed and released before you enter the next character. For the PC, the Meta key is usually the Alt key.

Moving the Cursor

Now that you know about the Control key, we can talk about the cursor-movement commands. The basic ones that you need to remember are

Ctrl+f Moves the cursor forward one character
Ctrl+b Moves the cursor back one character
Ctrl+-p Moves the cursor to the previous line
Ctrl+n Moves the cursor to the next line
Ctrl+-a Moves the cursor to the beginning of the line
Ctrl+e Moves the cursor to the end of the line

Most implementations of emacs conveniently map the first four movement commands to the arrow keys on the keyboard. Let’s edit a new file called asong2. Start emacs by entering the following command from the shell:


emacs asong2<Enter>

Now enter the following text into the buffer:


This is a file for edit

And you have to give emacs some credit

It’s really quite swell

And all you have to do is spell

emacs works, if you let it!

Now use the Ctrl+b command to move back through this lovely piece of poetry. Notice how the cursor jumps to the end of each line after reaching the beginning of the previous line. This works the same way in the opposite direction using the Ctrl+f command.

Another useful way of moving around is by scrolling through a file one screen at a time. The command Ctrl+v moves the cursor forward one screen at a time. The command META+v moves the cursor in the opposite direction.

Like vi, emacs treats a sequence of non-whitespace characters as a word. You can move the cursor forward one word at a time with the META+f command. The META+b command moves back one word.

Quitting emacs

At this time, you can stop editing to save the contents of the buffer to your file asong2. To do this, issue the command sequence Ctrl+x Ctrl+s. As you enter this command, notice how the command displays on the echo line as you type it. To quit emacs and return to the shell, enter the command Ctrl+x Ctrl+c. If you make changes that haven’t been saved using Ctrl+x Ctrl+s, emacs will ask for confirmation before quitting.

Deleting Text

You can delete text in several ways. The Backspace (or Delete) key is used to erase the character that precedes the cursor. The command Ctrl+d deletes the character underneath the cursor, and Ctrl+k deletes, or “kills,” all characters from the cursor to the end of the line. Words can be deleted also: META+d deletes the word the cursor is currently located over, and META+Del (the Delete key) deletes the word just before the current word.

If you ever find that you have done an edit that you didn’t want, simply press Ctrl+x u to undo the previous editing changes.


Tip:  
Change your mind about a command? Press Ctrl+g to abort the current command operation.


Previous Table of Contents Next