How Automatic file name completion works in Linux
Working at the Linux command line requires a lot of typing. It would get very frustrating with long commands and file names, but luckily your Tab key can make your life a lot easier.
You have to type only the beginning of a command, directory, or a file name, hit the Tab key and the shell completes the rest! For example, if you want to view a file called unBELIEVABLY-LongANDstupid.FileName.txt, all you have to type is something like:
$Â less un
and hit the Tab key. The rest of the monster file name is completed automatically and you don’t necessarily have to even remember the whole file name! It’s that simple.
There may be several files that start with the same letters but have different endings. How the shell behaves when you press the Tab key in this case, may differ depending on the shell and how it’s configured. Usually with a default bash configuration, when you’ve typed the first letters of a file name and hit the Tab key, the shell completes as much as it can and beeps. When you press the Tab key again, the shell shows you all the alternatives. Sometimes you’ll have to hit Tab twice before it shows you the alternatives.
Now you can either type the whole file name, or type a couple more letters and hit Tab again.
We’ll have an example: suppose you have files called unBELIEVABLY-LongANDstupid.FileName.txt and unBELIEVABLEfile.txt and you want to view the other file. Again, you try typing
$Â less un
and hit the Tab key. However, now the shell completes as much as it can and beeps:
$Â less unBELIEVABL
Now if you hit the Tab key again, either once or twice, the shell shows you the alternatives:
$Â less unBELIEVABL
unBELIEVABLY-LongANDstupid.FileName.txt unBELIEVABLEfile.txt
Now you can type
$Â less unBELIEVABLY
and hit Tab again. Now the shell knows which file you mean and completes the rest.
The shell you are using may behave a bit differently, so go ahead and experiment with your command line for a while. Pretty quickly you’ll get the hang of this feature, you’ll start using it all the time, and you won’t survive without file name completion anymore!
