Thursday, May 8, 2014

Tab Complete Is Awesome!

Tab Complete is AMAZING and will literally save your life!

Ok maybe not literally but still this is something that anyone working on the command line should know!

When you are on a command line and you start to type a command you can hit the tab key once and if there is only one match it will complete what you are typing. If you hit the tab key once and nothing happens you can hit it one more time and it will show you the current matching possibilities. This is a built in feature! For example lets say we have a directory that has the following contents:  2 directories: 

dir1 whatWasIThinkingWhenIPickedSuchALongNameForADirectory 

and a couple of files:

file1  file2 file3

if you were wanting to change directory into the second directory, you would normally have to type out the following:

cd whatWasIThinkingWhenIPickedSuchALongNameForADirectory

Using Tab completion you can type:

cd  w

And then hit the Tab key and it will fill out the rest of the directory for you!
if you just type cd and then hit tab, nothing will happen. If you hit the tab key twice it will show:

dir whatWasIThinkingWhenIPickedSuchALongNameForADirectory 

Notice that it doesn't show the files with it, just the directories. When you hit tab after cd it knows that the command cd takes a directory so it filters out the files. If your current directory had only 1 directory you can type cd then hit Tab and it will complete the directory.

Tab complete works with all commands! If there were multiple matches then Tab complete will complete the item that you are typing until there is a conflict. If you look at our first environment with 3 files and two directories as an example, you can type:

cat f

Then hit tab. This is equivalent to saying, "I want to see the contents of a file that starts with an 'f' but I can't remember what I called it". Since we have 3 files that start with an f and the only difference in the name is the number at the end (file1 file2 file3), hitting tab will then fill in as much as it can.

cat file

And then you can add the number. When is tab complete most useful? All the time! Especially when you are moving around deep directories. Let's say I have a bunch of directories dir1 dir2 .... And one of them has a directory called secretTreasure the reset are empty. I want to move into the secretTreasure directory, because let's face it treasure is awesome. Without Tab complete you will have to cd dir1 and then ls to see if the right directory is there. Then go to the second directory and continue. Or with Tab complete you can type cd dir1 and then hit tab once if it doesn't show up then change the 1 to a 2 and hit tab again, rinse and repeat until you find it, saving tons of key strokes.

I use this all of the time especially if I am going through multiple levels of directories to do something:

cp Downloads/folder/fileThatIWant.txt Documents/classes/eecs101/lectureNotes/

Hitting Tab at each level ensures that I am at a place that exists and that I haven't misspelled the directory name, or messed up the capitalization. If I start to type a directory and hit tab and nothing happens I can hit tab again, if nothing shows up then I know that I am not where I should be, so I remove what I was typing and hit tab twice and see what my options are.