Background
When you first had a computer, how did you use it? Some of that will depend on when you were born. My family first purchased a computer (custom built IBM Compatible Clone!) when I was 9, in 1992. It did not have Windows. It was DOS.
DOS was Microsoft’s version of UNIX.
One time, I thought it would be funny to delete the contents of my computer’s autoexec.bat file. This was basically a set of plain text instructions for what the computer should do when you turned it on. Turns out “nothing” is what happens when you delete those contents. Literally. We had to call a tech-savvy neighbor who brought a copy of his file to rewrite ours.
At 9 years old I was opening files, making new directories, and searching all from the DOS prompt. But then Windows happened, I got into Apple products, and was spoiled forever. Typing clunky commands seemed pretty archaic compared to pointing at what you want and getting it on demand, and less precise. I thought UNIX would be lost on me forever.
Enter the Terminal
^This is a terminal. People are so afraid of Terminal they call this thing Grand Central Station instead.
This, it turns out, was incorrect thinking. Windows and OSX are the bouncers of the computing world. They tell you what you can do and how to do it. Terminal, on the other hand is like the guy who works at the bar, lets you in when the place is closed and lets drink whatever you want.
The biggest benefit is speed.
Most people in Flatiron School know about tab completion at this point. Quite simply, while in a directory, start typing the name of a file or folder, hit tab, and Terminal will finish your command for you. From your home directory, “$cd doc” hit tab becomes “$cd Documents” pretty quickly. If you have multiple files that start with similar strings, there is a helper shortcut for you too. Option+ Tab will give you a list of folders and files in your directory starting with a string such that “$cd d” option+tab will yield Desktop/ Documents/ Downloads/ Dropbox/ dev/ so you’ll at least know what you’re working with.
The real fun is the speed and convenience that comes from automation. With a few quick key commands, custom Bash scripts, and aliases. You can save yourself major time. Let’s look at a few by opening up our bash profile.
Building our own scripts
- cd ~
- “What was the name of that file? types ls”
- “Hmm, must be a hidden file” types ls -a
- OK. subl .bash_profile
- “Man that was annoying, and now I’ve forgotten what directory I used to be in.”
We could simply type subl ~/.bash_profile but even that get tedious. So let’s add this function to our profile. Now when we type “subl-bash” anywhere in Terminal. And guess what, tab completion works for functions too.
1 2 3 |
|
That was fun. Let’s make another one. What’s something you want to do almost every time you make a new directory? Generally its navigate into that directory.
- mkdir my-awesome-directory
- cd my-awesome-directory
I think we can cut that down to one line of code. Like ruby, Bash functions can take an argument (), and then we access them in our function with $. So “mkcd directory name will now create a directory and navigate to it one shot.
1 2 3 4 |
|
What’s another thing we do as Flatiron students literally every day? We clone a git repository, try to remember the name of repo, and then navigate into that directoy. With this script, utilizing the bash command ‘basename’ [1] you can clone and navigate with a single command. Use this script by simply typing “gcl” and pasting the address of the github page you want to clone.
1 2 3 4 5 6 7 8 9 |
|
This one’s my favorite so far. First, install the chrome-cli gem. This gives you command line access to chrome.
Then, add this function to your .bash_profile. Enter “labs” into Terminal and in one shot we navigate to our labs directory, close whatever nonsense we have open in Chrome, and go to the Ironboard login page.
1 2 3 4 5 6 |
|
This next one navigates us to my blog’s directory, opens the posts folder in sublime, opens chrome to my local preview, and generates a local preview of the blog. Your function might have to change a bit if you are using a different blogging platform. Good thing Octopress lets use the command line!
1 2 3 4 5 6 |
|
And for good measure, let’s add some custom aliases.
1 2 3 |
|
Now we have one word shortcuts to get our most commonly used directories. Think how much time we can save just over the course of the semester with a few of these shortcuts. The Terminal is our oyster.
References:
1. http://unix.stackexchange.com/questions/44735/how-to-get-only-filename-using-sed