Course Links

Exercises

Resources

External

Overview

Unix was designed and developed by lazy people. Being lazy, they didn't like to type very much, which is why all the most commonly used commands all have just two letters apiece (mv, cp, rm, cd, etc.). But what's the point of short command names if your file names are long and descriptive (as they should be)? This document describes some other tricks to help you avoid typing as much, and thus to help you work faster.

Shell History

The window in which you type unix commands (after the $ command prompt) is called a shell. There are various different versions of the shell, but they all have features in common. For one thing, they keep a history of the commands you have typed. You can see this history by typing

$ history

at the command prompt. You can also access items in the history, in several ways. For example,

$ !

repeats the last command. If you want to be more specific, you can give the first few letters of the command you wish to repeat.

$ !em

would repeat the most recent command beginning with the letters em (probably an invocation of emacs).

You can also use the up- and down- arrow keys to scroll backwards through the history. Pressing up-arrow once will show the last command executed, twice will show the second-to-last command, etc. If you want to execute a command with slight modifications, you can hit up-arrow until you see the command you want to start with, then use the right- and left-arrow keys to edit the line. Hit return when the command looks like what you want, and it will be executed.

Tab Completion

Many shells (and also the open-file command of emacs, accesible via C-x C-f) allow a time-saver called tab-completion. Here's the idea: the computer knows what the files in a particular directory are named. So if you're going to refer to a file, there's no need to type out the full name if the computer can guess it from the first few letters you've typed. You only have to type out enough letters to uniquely identify the file, and then hit the TAB key. The shell will fill out the rest of the filename. If more than one file starts with the letters indicated, the shell will fill out as much as it can (if they files in question have more letters in common than those typed). You can then type a little more to indicate the file you want, and hit TAB again.

The example below shows for a hypothetical interaction what the computer would do when the TAB key is hit. Characters automatically added by the shell are in red. Characters typed by the user are in green.

$ ls
testDice.cpp  Dice.cpp      Dice.h        makefile
$ ls testDice.cpp
testDice.cpp
$ ls Dice.cpp
Dice.cpp      Dice.h
$