Rerunning Commands History

As you work in a shell, chances are you'll find yourself wanting to issue the same commands, or minor variants of them, again and again. For instance, you might try to install a package (as described in Chapter 11, "Managing Packages"), only to discover that you first need to add another package. After you add the depended-upon package, you'll want to add the first one. You can use a popular shell feature to simplify this task: history. Most modern shells maintain a record of the most recently typed commands. You can move through these commands by pressing the Up Arrow and Down Arrow keys. For the benefit of Emacs fans, the Ctrl+P and Ctrl+N keys perform these same actions. When you find the command you want to use, press the Enter key or edit the command, as described in the next section, "Modifying Commands: Editing."

You can also search through the history. Type Ctrl+R, and bash will display the following prompt:


Type the first few characters of the command you want to find, and the system will locate the best match. Type Ctrl+R again to search further back in the history.

Note In theory, Ctrl+S should perform a forward search; however, many text consoles intercept this keystroke as a means of pausing output, so Ctrl+S doesn't have the desired effect. Modifying Commands: Editing

Shells include simple editing features for commands you type. You can use these features to correct errors as you type commands, or you can use them to modify commands retrieved from the shell history, further expanding the utility of the history feature. The editing features in bash are modeled after those in Emacs. Table 4.1 summarizes the most important of these commands.

Table 4.1: Common bash Editing Commands




Move cursor to start of line.


Move cursor to end of line.

Ctrl+F or Right Arrow

Move cursor one position right.

Ctrl+B or Left Arrow

Move cursor one position left.


Move cursor one word right.


Move cursor one word left.


Delete the character to the left of the cursor.

Ctrl+D or Delete

Delete the character under the cursor.


Transpose the character on which the cursor rests and the preceding character.


Transpose the word on which the cursor rests and the preceding word.


Delete from the cursor to the end of the line.

Ctrl+X Backspace

Delete from the cursor to the start of the line.

Note Some editing features depend on appropriate keyboard handling, as described in

Chapter 3, "Using External Peripherals." In particular, some keymaps may not handle the Backspace and Delete keys in the way described here. Also, some keyboards label the Backspace and Delete keys differently; for instance, most Macintosh keyboards label Backspace as Delete, and Delete as Del.

Mastering these commands can be quite useful. For instance, suppose you discover that you've mistyped the second character of a command. Instead of retyping the entire command, you can press Ctrl+A to move the cursor to the start of the line, move the Right Arrow key to move forward one character, press Delete to delete the flawed character, type the correct character in its place, and press the Enter key to enter the new command. Instead of retyping potentially dozens of characters, your correction takes just five keystrokes.

0 0

Post a comment