Saturday, April 24, 2010

Question about text from a bash book about vi-mode

Below is a chunk of text partially from page 41 and 42 from "Learning
the bash Shell 3rd Edition" and it is talking about being in vi-mode.
It seems to be describing things the opposite of what is correct. Some
of it doesn't make any sense at all. The header is, Moving Around in
the History List, but it is talking about movement commands not the
command history. The description of the / and ? search commands are
reversed and the description of the G command seems a little off. When
I think of command history, I think of typing : and then using the up
arrow to see what I have already typed while editing the file. I tried
what they said. I typed :3k<Enter> and got error E471 which says an
argument is required. Am I right or wrong in my understanding of this
text from the book?

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 in
Table 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
/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 or 3- 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 as h, j, k, and l for 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 developed vi to
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's h, j, k, and l keys had little arrows on them,
so Joy decided to use those keys for appropriate commands in vi.
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:

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

Type w to get to the -, then l to get to the l. 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 by man or ma. To be on the safe side, you can also type
^ma; the ^ means match only lines that begin with ma.* 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 RETURN will accomplish the same thing. The G
command 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 "Prompting variables." 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.


--
If we can but prevent the government from wasting the labours of the
people, under the pretence of taking care of them, they must become
happy. - Thomas Jefferson

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Subscription settings: http://groups.google.com/group/vim_use/subscribe?hl=en

No comments:

Post a Comment