Command History and Tab Completion


It does not take long before the thought of typing the same command over and over becomes unappealing.

One minor typing error can ruin lines of a command. One solution is to use the command line history. By scrolling with the up arrow and down arrow keys, you can find plenty of your previously typed commands.

Try it by taking a look again at sneakers.txt (created in earlier examples in this chapter). The first time, however, at the shell prompt, type:

cat sneakrs.txt

Nothing happens, of course, because there is no sneakrs.txt file. No problem. We will just use the up arrow key to bring back the command and then use the left arrow key to get to the point where we missed the "e." Insert the letter and press Enter again.

We now see the contents of sneakers.txt.

By default, up to 500 commands can be stored in the bash command-line history file.

Tip

By typing the env command at a shell prompt, we can see the environment variable that controls the size of the command-line history. The line that reads HISTFILESIZE=500 shows the number of commands that bash will store.

The command-line history is actually kept in a file called .bash_history in your home directory. You can read it in a number of ways: by using pico (or emacs or vi), cat, less, more, and others.

Be aware that the .bash_history file can be quite long. To read it with the more command, from your home directory type:

more .bash_history

To move forward a screen, press the spacebar; to move back a screen, press b; to quit, press q.

To find a command in your history file without having to keep hitting the arrow keys or page through the history file, use grep, a powerful search utility (see the “The grep Command” section earlier in this chapter for more details). Say you are searching for a previously used command that is similar to cat sneaksomething.

Say you have used the command fairly recently and you think it might be in your history file. At the shell prompt, type:

 history | grep sneak 

Another timesaving tool is known as command completion. If you type part of a file, command, or pathname and then press the Tab key, bash will present you with either the remaining portion of the file/path, or a beep (if the beep sound is enabled on your system). If you get a beep, just press Tab again to obtain a list of the files/paths that match what has been typed so far.

For example, if you forget the command updatedb, but remember a portion of the command, you can su to root and then type up at the shell prompt, press the Tab key twice, and you will see a list of possible completions including updatedb. By typing the partial command upd and pressing Tab again, your command is completed for you.




The Red Hat Documentation Team - Official Red Hat Linux User's Guide
The Red Hat Documentation Team - Official Red Hat Linux User's Guide
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net