Archive for November, 2006

Command history

If you're new here, you may want to subscribe to my RSS feed.

Besides tab completion, another way to cut down on the amount of typing you do in Unix is the history command. Open Terminal, type history and press Enter. You’ll see a list of Unix commands you previously typed. If you’d like to rerun a command you previously used, you could of course Copy and Paste, or you can type an exclamation point (!) and the number of the command. For instance, if you see that the 10th command in your history is ls -al and you want to run it again, you can type !10 and that command will be re-executed.

If you want to simply rerun the last command, you can type !!.

You can also scroll through the command history by pressing the Up and Down arrows at a blank prompt. As soon as you find a command you want to rerun, you can edit it as needed or simply press Enter.

Tab completion, part 2

In the last post we discussed how tab completion makes typing Unix commands much easier. The downside, as Janssen pointed out, is that tab completion is case-sensitive, so typing cd doc and hitting tab won’t complete cd Documents/. But there’s a way to change this.

In your home folder you’ll find a file called .profile that contains settings for working in the bash shell (which is what you do every time you use Terminal.) It’s sort of the “preferences” file for the command line. The .profile file can do a lot of things, one of which is making tab completion case-insensitive. (We’ll cover more uses of .profile later.)

Because .profile begins with a dot, it’s invisible to most Mac OS X applications. A notable exception is Smultron, a free text editor that has an “Open Hidden” option. You can either use Smultron to open .profile in your home folder, or open Terminal and type open .profile, which should open the file in Apple’s TextEdit. If you don’t have this file, you’ll just create a new empty file by the same name and save it.

The line you’ll add to .profile is the following:

bind "set completion-ignore-case on"

If your .profile contains other text, simply put this at the end on its own line. If you’re creating .profile from scratch, this is the only line you’ll need. Save and close .profile.

After closing and reopening Terminal, your tab completion should now be case-insensitive. If you type cd doc and hit tab, it should fill in cd Documents/.

Tab completion

Tab sodaAs Nathan Sweeney mentioned in the comments of the last post, there is actually a faster way of “typing” Unix commands: the tab key. Hitting the tab key causes Unix to fill in as much of whatever you’re typing as possible. For example, if you open Terminal (you’ll begin in your Home folder) and want to change to your Documents folder, you’d normally type cd Documents. But it’s much easier with tab completion: type cd Doc and hit tab. You’ll notice that Unix fills in the rest of the word “Documents”.

You’ll also notice that tab completion only works when whatever you’re typing is unique. (Or rather, it only works to the degree that it is unique.) For example, if you type cd D from your home directory and hit tab, Unix won’t fill in anything because it doesn’t know whether you want cd Desktop or cd Documents. But if you hit tab a 2nd time, it will display Desktop and Documents to show you your options. Hitting tab once always causes Unix to fill in as much as it can. Hitting tab the 2nd time always shows you your options, if any.

You can even use tab completion to select a command. At the beginning of a command prompt, hitting tab twice will cause Unix to display ALL the commands that are available to you. There will probably be so many that Unix will ask you if you really want to see them all. Hit “y” to confirm, and then hit “q” (quit) when you’re done.

Tab completion makes it much easier to work with Unix.