Tab completion



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

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.

8 Responses to “Tab completion”

  1. Glenn Kerbein Says:

    When presented with multiple chioces from tab-completion, you can hit the ‘esc’ key. And tab-completion is not restricted to UNIX, nor does it belong to UNIX, that feature is part of bash, the Bourne-again Shell. You can use tab-completion for full document names, folders, or commands themselfs (for long commands like apcachectl). You can use an asterisk whereever it is appropriate, like when you have a folder of .txt files and want to cat them together, you can issue something like cat cat-me-*.txt > big-file.txt.

  2. Janssen Says:

    This will definitely help me learn the syntax. Does it work with extra flags? Although, maybe that wouldn’t be very helpful since most flags are a single letter.

    After playing with this, I noticed a couple of things:

    This feature is case sensitive, which is annoying for the cd command… others may prefer that. My second observation helps solve that problem…

    A third tab (after a unique command has been selected) prints the contents of the working directory (including invisible files, just like an ‘ls -A’ command) and brings you back to where you were in entering the command. This will be very useful for those times when I forget the exact name of the file or folder I want to target with the command.

  3. JP Says:

    Great tip!! I didn’t know TAB TAB would display available commands. I’ll be sure to use that if I’m running bash or csh.

    For what it is worth TAB is not how it is done in all *nix shells.

    ksh comes to mind that doesn’t use tab, unfortunately I don’t know what key it uses to do file/command completion. Rather, I typically use wildcards to get to where I want to go.

    For example,
    cd ~/Doc*/Sch*/Fa*06/C*440

  4. richard Says:

    Glenn & JP: Thanks for the correction about the tab completion being a part of bash, not all Unix shells. Also, the use of wildcards is important. I hope to fully cover that as well.

    Janssen: In the next post I’ll explain how to make tab completion case insensitive, which should make it even easier to use!

    Thanks for the comments.

  5. FreeMacUnix » Blog Archive » Tab completion, part 2 Says:

    […] 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. […]

  6. FreeMacUnix » Blog Archive » Command history Says:

    […] 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. […]

  7. Eric3 Says:

    By adding the line
    bind ‘”\t”:menu-complete’
    to your .profile, any time you hit tab on an ambiguous entry, it will start to cycle through the matches in the directory. Just keep hitting tab until you see the filename you want, then continue typing.

  8. Car Loan Says:

    Tab completion very much will be incredible business and you expressed astonishingly; thanks for the info!

Leave a Reply