Each of these types of status-line commands must be entered by pressing Return. This is not true for other types of vi commands, such as the ones that do insertions.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| h | Moves the cursor one space to the left. |
| j | Moves the cursor down one line. |
| k | Moves the cursor up one line. |
| l | Moves the cursor one space to the right. |
These keys can perform their operations only when vi is in command mode. For convenience, most implementations of vi map these keys to their directional counterparts on the keyboard arrow keys.
vi enables you to move through a file in bigger "leaps" as well. Following are some commands for scrolling more than one line at a time:
| Ctrl-U | Scrolls up a half-screen. |
| Ctrl-D | Scrolls down a half-screen. |
| Ctrl-F | Scrolls down one full screen. |
| Ctrl-B | Scrolls up one full screen. |
The size of these movements largely depends on the terminal settings.
It is also possible to move the cursor to a specific line in a file. If you want to move to the tenth line, type 10G or :10 in command mode. G by itself will move the cursor to the end of the file. The cursor will not move if the number given is not
applicable (for example, typing :10 in a eight-line file will have no effect).
vi will also enable you to move the cursor a word at a time. A word is defined as any sequence of non-whitespace characters. To move to the beginning of the next word or punctuation mark on the current line, type w. Type b to move the cursor to the
beginning of the current or previous word or punctuation mark.
vi has commands for deleting characters, lines, and words. Deletion means that the selected text is removed from the screen but is copied into an unnamed text buffer from which it can be retrieved.
To delete a word, use the dw command. If you want to delete the word to the right of the cursor, type dw. If you are in the middle of a word, it will delete from the cursor position to the end. You can also delete several words at a time. For example,
the command 4dw will delete the next four words on the current line.
Lines can be deleted individually or by specifying a range of lines to delete. To delete the current line, type dd. The command 4dd deletes four lines (the current line and three below it). dG will delete all lines from the current one to the end of the
file.
On the current line, you can delete in either direction: d^ will delete backward to the beginning of the line; d$ (or D) will delete forward to the end of the line.
To delete individual characters, x deletes the character underneath the cursor, and X deletes the character to the left of the cursor. Both of these commands will accept a number modifier: For example, 4x deletes the current character and the four
characters to the right.
Unwanted changes such as deletions can be immediately undone by the u command. This "rolls back" the last edit made.
Not sure what command you just typed? When in doubt, press Esc and then enter the command again.
Moving sections of text around in a file basically requires three steps:
Yanking text means to copy it into either a named or unnamed buffer. The unnamed buffer is a temporary storage space in memory that is continually overwritten by successive yanks. vi has 26 named buffers that correspond to each letter of the alphabet.
To yank the current line into the unnamed buffer, the command is yy or Y. These commands can be modified by a number indicating how many lines beneath the cursor are to be yanked. For example, the command
3yy
in your file asong (with the cursor on the top line) yanks the following text into the temporary buffer:
Down I walk by the bay, Where I can
This text could also be yanked into the named buffer a by the following command:
"a3yy
The yank command to overwrite the contents of the named buffer a. If you had typed a capital A instead of a lowercase a, the three lines would have been appended to the end of the a buffer. This overwrite-versus-append concept works the same for all of
the named buffers.
If you move the cursor to the end of the file using the :$ command, you can then paste the contents of the unnamed buffer to the end of the file. This is done using the p command, which pastes the contents of a buffer to the right of the cursor (P
pastes to the left of the cursor). The paste command can also specify a named buffer in the same way as the yank command:
"ap
Yanks can also be performed on words using the command yw. This command can also use named buffers and accepts numeric modifiers.
Text searches in vi can be performed in either direction: forward or backward. Searches are always started from the current cursor location and continue from the top or bottom of the file depending on which direction you use. In other words, searches
"wrap around" the file.
You can use your file asong to illustrate searches. To search forward through asong for the word "bay," you would type
/bay
and press Return. Notice that this is a status-line command. The command /bay is echoed on the status line and the cursor is moved to the first occurrence it finds in the forward direction of the string "bay." Interested in finding another
instance of "bay"? Enter a / character. This command continues the search for "bay" in the forward direction and places the cursor at the next instance of "bay." Each time you enter the / key, vi will try to find an instance
of the previous string pattern. When it reaches the end of the file, vi will loop back and continue its search at the start of the file.
You can also search backward for strings in vi by using the ? command. It works in exactly the same manner as the / command, but in the opposite direction. Try it out by typing
?I
in asong, instructing vi to search back for instances of "I." This search can be repeated by typing ?, as you may have suspected. You can continue a search by pressing n, which always continues a search in the same direction as the previous
search. However, typing N will use the same search string but in the opposite direction.
As I mentioned earlier, searches can be made very powerful through the use of regular expressions. The search command is supplied in the same fashion as described before (/ or ?), but square brackets are added to instruct vi to do a regular expression
expansion of the enclosed characters. For example, search forward through asong from the first line for all strings containing the substring "er". Type
/er
vi's first matching string arrives at "Where." If you type n, vi will move the cursor to "where," and so on. You can also specify collections of characters or ranges of characters to match. Try typing the following:
/[a-z]y
This command used in asong will find the strings "by" and "my," as well as any word with these strings inside them (such as "bay"). This works because the range of characters given are treated as an enumerated range of
ASCII values. Thus, you could also include a range of numbers (for example, 0-9). Now try the following command:
/[Mm]y
This will locate the strings "My" and "my."
In vi, searches without regular expressions will find only exact matches of the supplied pattern (including the case of the letters in the pattern). Clearly, regular expressions can be used to enhance many types of searches in which you may not know
exactly how a pattern appears in a file.
One of the more common applications of a search is to replace instances of one word (or pattern) with another. This is done with an ex command that starts with a colon. To search the entire asong file for the string "Down" and replace it with
the string "Up," type
:%s/Down/Up/g
The s indicates that this is a search operation, the % means that the entire file is to be searched, "Down" is the pattern to be found, "Up" is the new pattern, and the g tells vi that the search should continue until there are no
more pattern matches. Without the g, vi would perform the replacement on only the first match it finds. This command also works with regular expressions appearing in the search pattern and the replacement pattern.
vi is configurable, which means that you can set options to control your editing environment. These options are initialized with default values that you can modify in vi at any time. vi is configured using the set command. The set command must be
preceded by a colon and entered by pressing Return. For example, to display line numbers in the editor, you would issue
:set number
The following table describes a few of the more common set commands.
| all | Displays a list of all available set options and their current status. |
| errorbells | Sounds the terminal bell when an error occurs. |
| ignorecase | Searches are case-insensitive. |
| number | Displays line numbers in the leftmost column of the screen (these are not written to the file). |
| showmode | An indication appears at the bottom right of the screen if you are in input mode, change mode, replace mode, and so on. |
set commands that do not take a value can be switched off by inserting a "no" as a prefix to the set parameter. For example, the command
:set nonumber
switches line numbering off. The command
:set
shows only the options that you have changed.
The settings that you use in a vi session are (unfortunately) lost each time you exit vi. If you do not like the idea of resetting these options each time you use vi, there is an easier way to perform this initialization. Use the vi initialization file
called .exrc. vi searches for this file in your home directory each time it is invoked. If it can't find this file, it uses the defaults set within the vi program. As you will see in the following example, the .exrc file can also be used to define vi
macros.
A sample .exrc file would look something like this:
set number set errorbells set showmode
Note that the colon is not required before a set command in a .exrc file.
The following 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.
| 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. |
| C-f | Scrolls forward one screen. |
| C-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. |
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.
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 will display 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 which
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.
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 I fill you in on this little detail, there are two keys that you
should know about: the Control key (which I will refer to as C) and the Meta key (denoted by M). 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 down 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.
You may see the Control key abbreviated as C and the Meta key denoted by M.
On the PC, you should use the Alt key for the Meta key.
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:
| C-f | Moves the cursor forward one character. |
| C-b | Moves the cursor back one character. |
| C-p | Moves the cursor to the previous line. |
| C-n | Moves the cursor to the next line. |
| C-a | Moves the cursor to the beginning of the line. |
| C-e | Moves the cursor to the end of the line. |
When I refer to a command such as C-b, I mean press and hold the Control key while you press the letter b. The same is true for Meta commands such as M-v.
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. (If you are in the middle of a previous file, exit the editor by typing Ctrl-X, Ctrl-C.) Start up a
new copy of 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 C-b command to move back through this horrendous piece of poetry. Notice how the cursor jumps up to the end of each line after the reaching the beginning of the previous line. This works the same way in the opposite direction using the C-f
command.
Another useful way of moving around is by scrolling through a file one screen at a time. The command C-v moves the cursor forward one screen at a time. The command M-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 M-f command. The M-b command moves back one word.
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 C-x C-s. As you enter this command, notice how the command is displayed on the echo line as you type it. To quit emacs and
return to the shell, enter the command C-x C-c. If you have made changes that haven't been saved using C-x C-s, emacs will ask for confirmation before quitting.
You can delete text in several ways. The Backspace (or Delete) key is used to erase the character immediately preceding the cursor. The command C-d deletes the character underneath the cursor, and C-k deletes or "kills" all characters from the
cursor to the end of the line. Words can be deleted also: M-d deletes the word the cursor is currently located over and M-Del (the Delete key) deletes the word previous to the current word.
If you ever find that you have committed an edit that you didn't really want, just type C-x u to undo the previous editing changes. You can repeat the undo command as many times as you want, rolling over all the changes you made. This is an advantage
over vi, which can only undo the last change.
Change your mind about a command? Type C-g to abort the current command operation.
emacs enables you to edit several files in one session, each contained within its own buffer. To copy an external file into a new buffer, use the C-x C-f command. After entering this command, you will see the following prompt on the echo line:
Find file: ~/
emacs is smart when it looks for files. It supports filename completion, which means that you can simply type a few characters of a filename and emacs will attempt to match a file (or files) to what you have typed so far. To do this, type in the letters
".log" and press the Tab key. emacs expands this to ~/.login (or any other filename that matches). If two or more files match the pattern supplied, pressing the Tab key will cycle through them.
After you have loaded a new file into emacs, you can switch between buffers by using the C-x b command followed by the name of the buffer that you want. The buffer's name is that of the file that was loaded into it. The C-x b command also uses filename
completion, so you can use the Tab key to cycle through your edit buffers after supplying a few relevant characters.
When you have finished editing a buffer, instead of saving the contents using the C-x C-s command, you may decide that you do not really want to keep the edits you have made. You can "kill" the current buffer by entering the command C-x k.
emacs will prompt you for the name of the buffer to kill, but you can kill the current buffer by simply pressing Return. emacs will ask for confirmation, to which you can respond by typing yes (if you're sure) and press Return.
Whenever you are working with just two buffers, you can simply press Return after entering the C-x b command to switch to the other buffer.
In order to copy and move blocks of text in emacs, you must define the region of text by marking the beginning and end points of the text block. This is done by moving the cursor to where you want the block to begin and marking it using the C-Space
command (in this case, Space means literally the spacebar). The end of the block is defined by wherever you place the cursor after that. To make a copy of the block, enter the command M-w. The text within the block is copied to emacs's internal clipboard,
from which it can be pasted at another location using the C-y command. Alternatively, you can cut the block into the clipboard using C-w instead of M-w. Cutting, of course, deletes the text from its current location.
Let's try out some of these techniques on your buffer asong2. Use the M-< command to jump to the beginning of the buffer. Enter a C-Space to mark the start of the block and then use C-n to move down a line. Cut the block to the clipboard using C-w,
move the cursor to the end of the buffer using M->, and paste it using C-y. The result should look like this:
It's really quite swell And all you have to do is spell emacs works, if you let it! This is a file for edit And you have to give emacs some credit
You can search forward and backward through text using the C-s and C-r commands, respectively. These commands, like many in emacs, use command completion. This is the same concept as filename completion: you supply a few characters and emacs tries to
fill in the rest. In this case, however, emacs moves the cursor to each instance it finds of the string supplied.
As you enter more characters, emacs narrows its search further. When you have found a correct match, press Return or use any of the cursor-movement commands to halt the search.
As with vi, searching in either direction wraps around the beginning or end of the file, depending on in which direction you are searching. However, when emacs reaches the top or bottom of the file, it will tell you that the search failed. You can keep
searching by pressing C-s or C-r accordingly and emacs will continue using the current string.
To illustrate how searching in emacs works, let's search backward through your file asong2. Enter C-r and type an s. emacs moves the cursor to the "s" in "works". Now type a w. emacs now tries to find a pattern that matches the
string sw. The cursor ends up on the "w" in "swell". You can edit the search string using the Backspace or Delete key. Delete the w and type a p. What happens?
Search-and-replaces are done by entering the query-replace command. This is qualified by the M-x command, which tells emacs that the text to follow is a full command and not a key combination. After you have entered the query-replace command, you will
be prompted for the string to be found. Enter the string and press Return. emacs will then prompt you for the replacement string. Once you have entered the replacement string, emacs will search for every instance of the first string and, if it finds one,
asks you if it should be replaced with the second string.
emacs is actually composed of a set of explicit command names that are bound to key combinations. The query-replace command is bound to M-% in some implementations of emacs.
emacs is versatile enough to handle many different types of editing chores. It enables you to associate modes to buffers so that you can have text formatting specific to your editing application. If you enter the command C-x m, emacs enters mail mode,
which formats a buffer with To: and Subject: fields as well as a space for the body of the mail message. emacs can even send the mail message for you (by entering C-c C-c) after you have finished editing it.
emacs also supports modes for many different programming languages such as C. When a file with the extension .c (C source code) or .h (C header file) is loaded into emacs, the buffer is automatically set to C mode. This mode has knowledge of how C
programs are formatted, and pressing the Tab key will indent a line correctly based on its place in the program (a for loop within another for loop, as an example).
One of the best features of the emacs editor is that if you ever get stuck, or are just plain overwhelmed by it all, help is just a few keystrokes awayand lots of it! If you need a short emacs tutorial, just enter C-h t. If you would like to find
out what function a particular key supports, type C-h k and then press the key. The help option has many different topics. Use C-h i to load the information documentation reader and read about all the types of help available.
emacs, like the vi editor, has such a rich command set that we can cover only a portion of it in this chapter. The following table is a summary of the strictly essential commands that you will need for basic editing in emacs. The emacs man page should
be consulted for a more comprehensive description of the full emacs command set.
| C-b | Moves back one character. |
| C-d | Deletes the current character. |
| C-f | Moves forward one character. |
| C-g | Cancels the current command. |
| C-h | Enters emacs online help. |
| C-n | Moves forward to the next line. |
| C-p | Moves back to the previous line. |
| C-s | Searches forward for a string. |
| C-v | Scrolls forward one screen. |
| M-v | Scrolls backward one screen. |
| C-x u | Undoes the last edit. |
| C-x C-c | Exits emacs. |
| C-x C-s | Saves the buffer to a file. |
There are many text editors available for the Linux system. Two of the most popular are vi (which is actually an alias to the elvis editor) and emacs. Both provide basic editing functions such as inserting and deleting text, reading and writing of external files, text searching, and copying and moving text. vi is a full-screen editor that has two modes: command mode and text mode. emacs is an extendible and powerful editor that is highly configurable to suit a variety of editing tasks (such as programming, document writing, and changing user or system files).