See and reuse the most recently typed commands

The command-line includes a powerful history feature that can make life much easier. To see the recently typed commands, type history. This simply dumps to screen a hidden file in your /home directory called .bash_history where up to 1000 commands are recorded. Because this list will scroll off the screen when listed it's a good idea to pipe the output into a text reader, such as less: $ history|less

To reuse one of your commands, at the command-prompt type an exclamation mark (!; known as a bang in bash-speak) and then the number alongside the entry in the history list. For example, on my system, I noted when viewing the history list that the command cp /etc/fstab -/Desktop was command 591. To use it again, I typed !591 at the command-prompt. If you ever need to simply repeat a command you've just used, type two exclamation marks—!!.

To actively rifle through your command history, hit [Ctrl ]+(T and then start typing the command you're interested in. The prompt will "auto-complete" as you type. To use the command, hit (Enter). To edit it before using it, hit (Esc) and then make your changes.

Hitting the up and down cursor keys will also let you move through the most recently commands typed. Just hit [Enter] when you find the one you want to reuse.

Report erratum

For more command-line productivity tricks, see in particular Tip 46, on page 109; Tip 56, on page 119; Tip 105, on page 157; Tip 192, on page 231; Tip 259, on page 299; and Tip 193, on page 232, amongst others.

Was this article helpful?

0 0

Post a comment