Terminal Tips: History and Auto-completion

These tips can be used with the OS X terminal, iTerm, and most Linux shells. I have also verified the history works with the Windows “DOS Prompt”. It’s nice when things work out in a consistent cross-platform way. 😉

The history feature is useful when a programmer wishes to perform similar commands over again. In the past the process used to be more complex. Now it’s as simple as using the up arrow key. The down arrow key works to go the other direction. Many of those just getting familiar with the various terminal or shell commands will at first find this feature somewhat dubious. Here is a situation where the history feature really shines. Picture writing a program for a couple of hours and saving it. When run it has a syntax error. By using the history the programmer can save keystrokes to get back to where they were before. A second example would be a long command with a mistake on the line. Not realising the mistake the programmer presses the <Enter> key. Naturally the program doesn’t do what was expected. The programmer can use the history to recall the previous command and figure out where the problem was, then correct it.

Using auto-completion falls into a similar category in terms of usefulness. At first the novice shell user may not find a use for it. Eventually they will come to rely on it. The auto-completion searches the $PATH system variable I mentioned in an earlier article to match a file or command which is partially typed on the command line. For example let’s say the programmer wants to view the contents of their Desktop directory. Naturally they would use the ls command to do so. The whole command would be ls Desktop. Instead of typing the whole thing the programmer could type ls De<Tab>. This will fill out the command correctly so they can then press the <Enter> key. The reason they would have to go as far as De instead of D is because there is another directory called Documents. In certain shells the auto-complete would cycle through the possibilities. In others it will not. It is best to assume it won’t.

Please follow and like us:

About the Author

BlankMike