Department of Engineering

IT Services

The Command History & Editing Commands

The shell keeps a record of the commands you use to help you avoid retyping things when the command you want to execute is similar to one you have used before. This record of the last few commands you have used is called your command history. Different shells offer different facilities for re-using old command lines.

You can always access the history using control characters or the arrow keys. To look at the last command in your history, press the up-arrow key. Doing this again takes you back over older commands. The down-arrow key displays more recent commands.

To execute an old command again use the arrow keys to display it then just press the <Return> key. If you decide that you don't want to repeat any of your old commands you can erase the command line with C-x.

Try a few simple commands. Type

sleep 1

Now repeat them in a different order using up-arrow to go back and select previous commands. The command hostname prints the name of the computer you are working on.

It is possible to modify old commands if you want to execute something similar to one of them. Pressing the left-arrow key moves the cursor backwards across the command, and the right-arrow key moves forward.

To add to a command you position the cursor at the point in the command you wish to alter using the left and right arrow keys, and type in the additional text.

To delete from a command you position the cursor in the same way and then use C-d to delete the character under the cursor. Furthermore C-k erases to the end of the current command line. You can also use the <Back Space> key on the keyboard to delete backwards from the cursor.

Pressing C-r produces the prompt `^R', if you now type a small part of a command you would like to repeat and press <Return> the shell will search back through your history for a command which contains the character string you supplied. If it finds the old command you wanted you can then edit it and/or execute it as described above. To summarise :-

  • up-arrow - Moves up the history to the previous command.
  • down-arrow - Moves down the history to the next most recent command.
  • left-arrow - Moves cursor back to the left over a command.
  • right-arrow - Moves cursor forward to the right over a command.
  • C-d - Deletes the character under the cursor.
  • C-k - Kills all the characters from the current cursor position to the end of the command line.
  • C-r string - Display the most recent command which contained string.