• Aucun résultat trouvé

Deletion Commands

Dans le document Learning the bash Shell, 3rd Edition (Page 141-149)

Now that you know how to enter commands and move around the line, you need to know how to delete. The basic deletion command in vi-mode is dfollowed by one other letter. This letter determines what the unit and direction of deletion is, and it corresponds to a motion command, as listed previously inTable 2-8.

Table 2-10shows some commonly used examples.

Table 2-10. Some vi-mode deletion commands Command Description

dh Delete one character backwards

dl Delete one character forwards

db Delete one word backwards

dw Delete one word forwards

Command Description

dB Delete one non-blank word backwards

dW Delete one non-blank word forwards

d$ Delete to end of line

d0 Delete to beginning of line

These commands have a few variations and abbreviations. If you use a c instead of d, you will enter input mode after it does the deletion. You can supply a numeric repeat count either before or after the d (or c).

Table 2-11lists the available abbreviations.

Table 2-11. Abbreviations for vi-mode delete commands

Command Description

D Equivalent tod$(delete to end of line)

dd Equivalent to0d$(delete entire line)

C Equivalent to c$ (delete to end of line, enter input mode)

cc Equivalent to 0c$ (delete entire line, enter input mode)

X Equivalent to dl (delete character backwards)

x Equivalent to dh (delete character forwards)

Most people tend to use D to delete to end of line, ddto delete an entire line, and x (as "backspace") to delete single characters. If you aren't a hardcore vi user, you

may find it difficult to make sure the more esoteric deletion commands are at your fingertips.

Every good editor provides "un-delete" commands as well as delete commands, and vi-mode is no exception.

vi-mode maintains a delete buffer that stores all of the modifications to text on the current line only (note that this is different from the full vi editor). The command u undoes previous text modifications. If you type u, it will undo the last change. Typing it again will undo the change before that. When there are no more undo's,bash will beep. A related command is . (dot), which repeats the last text modification command.

There is also a way to save text in the delete buffer without having to delete it in the first place: just type in a delete command but use y ("yank") instead of d. This does not modify anything, but it allows you to retrieve the yanked text as many times as you like later on. The commands to retrieve yanked text arep, which inserts the text on the current line to the right of the cursor, and P, which inserts it to the left of the cursor. The y, p, andP commands are powerful but far better suited to "real vi"

tasks like making global changes to documents or programs than to shell commands, so we doubt you'll use them very often.

Moving Around in the History List

The next group of vi control mode commands we cover allows you to move around in and search your command history. This is the all-important functionality that lets you go back and fix an erroneous command without retyping the entire line. These commands are summarized inTable 2-12.

Table 2-12. vi control mode commands for searching the command history

Command Description

k or - Move backward one line

j or + Move forward one line

G Move to line given by repeat count

Command Description

/string Search backward for string

?string Search forward for string

n Repeat search in same direction as previous

N Repeat search in opposite direction of previous

The first two can also be accomplished with the up and down cursor movement keys if your keyboard has them.

The first three can be preceded by repeat counts (e.g.,3k or3-moves back three lines in the command history).

If you aren't familiar with vi and its cultural history, you may be wondering at the wisdom of choosing such seemingly poor mnemonics ash,j,k, andlfor backward character, forward line, backward line, and forward character, respectively. Well, there actually is a rationale for the choices—other than that they are all together on the standard keyboard. Bill Joy originally developedvito run on Lear-Siegler ADM-3a terminals, which were the

first popular models with addressable cursors (meaning that a program could send an ADM-3a command to move the cursor to a specified location on the screen). The ADM-3a'sh,j,k, andlkeys had little arrows on them, so Joy decided to use those keys for appropriate commands invi. Another (partial) rationale for the command choices is that CTRL-H is the traditional backspace key, and CTRL-J denotes linefeed.

Perhaps + and - are better mnemonics than j and k, but the latter have the advantage of being more easily accessible to touch typists. In either case, these are the most basic commands for moving around the history list.

To see how they work, let's use the same examples from the emacs-mode section earlier.

You enter the example command (RETURN works in both input and control modes, as does LINEFEED or CTRL-J):

$ fgrep -l Duchess < ~cam/book/alice_in_wonderland

but you get an error message saying that your option letter was wrong. You want to change it to -s without having to retype the entire command. Assuming you are in control mode (you may have to type ESC to put yourself in control mode), you type k or - to get the command back. Your cursor will be at the beginning of the line:

$ [f]grep -l Duchess < ~cam/book/alice_in_wonderland

Typewto get to the-, thenlto get to thel. Now you can replace it by typing rs; press RETURN to run the command.

Now let's say you get another error message, and you finally decide to look at the manual page for the fgrep command. You remember having done this a while ago today, so rather than typing in the entire man command, you search for the last one you used. To do this, type ESC to enter control mode (if you are already in control mode, this will have no effect), then type / followed byman or ma. To be on the safe side, you can also type^ma; the ^ means match only lines that begin withma.[7]

But typing/^ma doesn't give you what you want: instead, the shell gives you:

$ make myprogram

To search for "man" again, you can type n, which does another backward search using the last search string.

Typing / again without an argument and hitting RETURNwill accomplish the same thing.

TheGcommand retrieves the command whose number is the same as the numeric prefix argument you supply. G depends on the command numbering scheme described in Chapter 3 Section 3.4.2.3. Without a prefix argument, it goes to command number 1. This may be useful to former C shell users who still want to use command numbers.

Character-Finding

Dans le document Learning the bash Shell, 3rd Edition (Page 141-149)

Documents relatifs