Keyboard advanced operating skills
I often kiddingly describe Unix as "the operating system for people who like to type." Of course,the fact that it even has a command line is a testament to that.But command line users don't like to type that much.Why else would so many commands have such short names like cp,ls,mv,and rm?In fact,one of the most cherished(珍爱的)goals of the command line is laziness;doing the most work with the fewest number of keystrokes(键击).Another goal is never having to lift your fingers from the keyboard,never reaching for the mouse.In this chapter,we will look at bash features that make keyboard use faster and more efficient.
The following commands will make an appearance:
clear - Clear the screen
history - Display the contents of the history list
Command edit
bash uses a library (a shared collection of routines that different programs can use) called Readline to implement command line editing.We have already seen some of this.We know,for example,that the arrow keys move the cursor but there are many more features.Think of these as additional tools that we can employ in our work.It's not important to learn all of them,but many of them are very useful.Pick and choose as desired.
Note:Some of the key sequences below (particularly those which use the Alt key) may be intercepted(拦截) by the GUI for other functions.All of the key sequences should work properly when using a virtual console.
Move cursor
The following table lists the keys used to move the cursor:
Table 9-1:Cursor Movement Commands
| Key | Action |
| Ctrl-a | Move cursor to the beginning of the line。 |
| Ctrl-e | Move cursor to the end of the line. |
| Ctrl-f | Move cursor forward one character;same as the right arrow key. |
| Ctrl-b | Move cursor backward one character;same as the left arrow key. |
| Alt-f | Move cursor forward one word. |
| Alt-b | Move cursor backward one word. |
| Ctrl-l | Clear the screen and move cursor to the top left corner.The clear command does the same thing. !!!!在RHEL上实验过不是这样的 |
Modify text
Table 9-2 lists keyboard commands that are used to edit characters on the command line.
Table 9-2:Text Editing Commands
| Key | Action |
| Ctrl-d | Delete the character at the cursor location |
| Ctrl-t | Transpace(exchange) the character at the cursor location with the one preceding it |
| Alt-t | Transpose the word at the cursor location with the one preceding it. |
| Alt-l | Convert the characters from the cursor location to the end of the word to lowercase |
| Alt-u | Convert the characters from the cursor location to the end of the word to uppercase |
Cut and paste text
The Readline documentation uses the terms killing and yanking to refer to what we would commonly call cutting and pasting.Items that are cut are stored in a buffer called the kill-ring.
Table 9-3:Cut And Paste Commands
| Key | Action |
| Ctrl-k | Kill text from the cursor location to the end of line. |
| Ctrl-u | Kill text from the cursor location to the beginning of the line. |
| Alt-d | Kill text from the cursor location to the end of the current word |
| Alt-Backspace | Kill text from the cursor location to the beginning of the word.If the cursor is at the beginning of a word,kill the previous word. |
| Ctrl-y | Yank text from the kill-ring and insert it at the cursor location. |
The Meta Key
If you venture into the Readline documentation,which can be found in the READLINE section of the bash man page,you will encounter the term "meta key." On modern keyboards this maps to the Alt key but it wasn't always so.
Back in the dim times(before PCs but after Unix)not everybody had their own computer.What they might have had was a device called a terminal.A terminal was a communication device that featured a text display screen and a keyboard and just enough electronics and just enough electronics inside to display text characters and move the cursor around.It was attached(usually by serial cable) to a larger computer or the communication network of a larger computer.There were many different brands of terminals and they all had different keyboards and display feature sets.Since they all tended to at least understand ASCII,software developers wanting portable applications wrote to the lowest common denominator.Unix systems have a very elaborate way of dealing with terminals and their different display features.Since the developers of Readline could not be sure of the presence of a dedicated extra control key,they invented one and called it "meta."While the Alt key serves as the meta key on modern keyboards,you can also press and release the Esc key to get the same effect as holding down the Alt key if you're still using a terminal(which you can still do in Linux!)
!!!!!!!!!!!!!!!!!表示看完也不晓得啥是元键
Completion(自动补全)
Another way that the shell can help you is through a mechanism called completion.Completion occurs when you press the tab key while typing a command.Let's see how this works.Given a home directory that looks like this:

Try typing the following but don't press the Enter key:

Now press the tab key:

See how the shell completed the line for you?Let's try another one.Again,don't press Enter:

No completion,just a beep.This happened becasue "D" matches more than one entry in the directory.For completion to be successful,the"clue" you give it has to be unambiguous.If we go further:

Then press tab:

The completion is successful.
While this example shows completion of pathenames,which is its most common use,completion will also work on variables(if the beginning of the word is a "$"),user names(if the word begin with "~"),commands(if the word is the first word on the line. 这句还是有毛病,用管道的话对这行中的第一个命令也能补全啊) and host names (if the beginning of the word is "@").Host name completion only works for host names listed in /etc/hosts.
There are a number of control and metra key sequences that are associated with completion:
Table 9-4:Completion Commands
| Key | Action |
| Alt-? | Display list of possible completions.On most systems you can also do this by pressing the tab key a second time,which is much easier. |
| Alt-* | Insert all possible completions.This is useful when you want to use more than one possible match |
Programmable Completion(可编程自动补全)
Recent versions of bash have a facility called programmable completion.Programmable completion allows you(or more likely,your distribution provider)to add addtional completion rules.Usually this is done to add support for specific applications.For example it is possible to add completions for the option list of a command or match particular file types that an application supports.Ubuntu has a fairly large set defined by default.Programmable completion is implemented by shell functions,a kind of mini shell script that we will cover in later chapters.If you are curious,try;
set | less
and see if you can find them.Not all distributions include them by default.
Use history command
As we dicovered in Chapter2,bash maintains a history of commands that have been entered.This list of commands is kept in your home directory in a file called .bash_history.This history facility is a useful resource for reducing the amount of typing you have to do,especially when combined with command line editing.
Search history command
At any time,we can view the contents of the history list by:

By default,bash stores the last five hundred commands you have entered.We will see how to adjust this value in a later chapter.Let's say we want to find the commands we used to list /usr/bin.One way we could do this:

And let's say that among our results we got a line containing an interesting command like this:

The number "34" is the line number of the command in the history list.We could use this immediately using another type of expansion called history expansion.To use our discovered line we could do this:
bash will expan "!34" into the contents of the thirty-fourth line in the history list.There are other forms of history history expansion that we will cover a little later. bash also provides the ablility to search the history list incrementally.This means that we can tell bash to search the history list as we enter characters,with each additonal character further refining our search type Ctrl-r followed by the text you are looking for.When you find it,you can either type Enter to execute the command or type Ctrl-j to copy the line from the history list to the current command line.To find the next occurrence of the text (moving "up" the history list),type Ctrl-r again.To quit searching,type either Ctrl-g or Ctrl-c.Here we see it in action:

First type Ctrl-r:

The prompt changes to indicate that we are performing a reverse incremental search.It is "reverse" because we are searching from "now" to some time in the past.Next,we start typing our search text.In this example "/usr/bin":

Immediately,the search returns our result.With our result,we can execute the command by pressing Enter,or we can copy the command to our current command line for further editing by typing Ctrl-j.Let's copy it.Type Ctrl-j:

Our shell prompt returns and our command line is loaded and ready for action!The table below lists some of the keystrokes used to manipulate(熟练操纵) the history list:
Table 9-5:History Commands
| Key | Action |
| Ctrl-p | Move to the previous history entry.Same action as the up now |
| Ctrl-n | Move to the next history entry.Same action as the down arrow. |
| Alt-< | Move to the beginning(top) of the history list. |
| Alt-> | Move to the end(bottom) of the history list,i.e.,the current command line |
| Ctrl-r |
Reverse incremental search.Searches incrementally from the current command line up the history list. |
| Alt-p | Reverse search,non-incremental.With this key,type in the search string and press enter before the search is performed |
| Alt-n | Forward search,non-incremental. |
| Ctrl-o | Execute the current item in the history list and advance to the next one.This is handy if you are trying to re-execute a sequenc of commands in the history list. |
The expansion of the history command
The shell offers a specialized type of expansion for items in the history list by using the "!" character.We have already seen how the exclamation point can be followed by a number to insert an entry from the history list.There are a number of other expansion features:
| Sequence | Action |
| !! | Repeat the last command.It is probably easier to press up arrow and enter |
| !number | Repeat history list item number. |
| !string | Repeat last history list item starting with string. |
| !?string | Repeat last history list item containing string. |
We would caution against(防备) using the "!string" and "!?string" forms unless you are absolutely sure of the contents of the history list items.
There are many more elements available in the history expansion mechanism,but this subject is already too arcane(晦涩难懂) and our heads may explode(爆炸) if we continue.The HISTORY EXPANSION section of the bash man page goes into all the gory details(血淋淋的细节).Feel free to explore!
Script
In addition to the command history feature in bash,most Linux distributions include a program called script that can be used to record an entire shell session and store it in a file.The basic syntax(语法) of the command is:
script[file]
where file is the name of the file used for storing the recording.If no file is specified,the file typescript is used.See the script man page for a complete list of the program's options and features.
Sum-up
In this chapter we have covered some of the keyboard tricks that the shell provides to help hardcore typists reduce their workloads.I suspect that as time goes by and you become more involved with the command line,you will refer back to this chapter to pick up more of these tricks.For now,consider them optional and potentially helpful.
浙公网安备 33010602011771号