Command me, Baby: 2. Learn to read and write
This post contains language intended for an audience over the age of 13.
Hey, welcome back. This is a 42 part, once-a-week series and if you haven’t been here before, you’ll want to read the older entries to have enough patience to get through the dense paragraphs below:
0. Learn to Love Your Shell
1. Exploring an Unfamiliar Land
So I know I said last week that you would learn how to install MySQL from source, but I might have Laurence Maroney’ed on that one. I forgot that you don’t yet know how to read and write within your shell.
As Charlie Day said: “Illiteracy? What does that even mean?!?”
But seriously, reading and editing text are the single most important tasks you perform as a programmer. Programming is like operating a guillotine in that there are two distinct phases: preparation and execution. It’s also like operating a guillotine when you have businesses depending on you because this isn’t a toy you’re dealing with — every time you flip a switch, heads can roll.

Let’s talk more about the actual execution, a passion I share with certain sixteenth century kings. The execution phase of any programming task consists invariably of editing text and executing commands. The ratio is probably 95% inspecting and editing text, 5% executing commands. The reason I’m teaching you commands first is that there are far more extensive and accessible resources on learning how to write any programming language than on how to use the shell. Also, all that editing means nothing if you can’t pull triggers. You’d be like the two of clubs in a spades game or the scout in Stratego — present but toothless.
But I realized while I was writing the MySQL post (not that I’m making this series up as I go along, whaaaaaat?) that if you can’t read and edit files, you’re like a paraplegic crocodile: you’ve got the teeth but you’re legless and armless. It’s like I’d have to put everything right in front of your mouth.
I mean, I’ll be holding your hand for a couple more posts, it takes a little longer to get comfortable enough to start exploring on your own. But, I want to shake your dependencies on my lead as soon as possible and get you exploring your own curiosity as soon as possible, looking up man pages and the like, because that’s how I learned all this shit in the first place — alone. Powered only by my curiosity and casual preference to continue receiving a paycheck.
So, you need to start getting to know how to use a text editor as Drake says “sooner than later”. As such I couldn’t justify going any further without teaching you how to use my best friend in the robot world, emacs:

Emacs is the tool I use to edit text. I’m not going to go too much into the history of it except to tell you that the official name is GNU emacs, where GNU is an infinitely recursive acronym that stands for GNU Not Unix. When I found that out I got almost as much of a nerd boner as I did for this Google suggestion for recursion.
OK enough history — just know that in my profession I spend more time in emacs than on the internet (not counting email). In fact I’m in it right now, writing this blog post for you.
Emacs is the crucial glue to a complex craft, and it takes years to master so I won’t be trying to explain all the features to you today. I just need you to be able to open up new or existing files, read them, change them, and save your changes.
Other Editors
I should also mention before we dive in that there are other options, feel free to check them out but know that all editing in this series will be done using emacs. The other editors I’ve seen people use effectively are vi and full-scale Integrated Development Environments (IDE) like Aptana, Eclipse, MonoDevelop, Netbeans or whatever.
We live in the time of a raging nerd-off in the form of an ongoing vi vs emacs debate. My understanding is this: emacs and its add-ons are pirates, vi is a ninja.

Let me explain. Emacs main selling point is that it is fully customizable if you know the esoteric, ancient, but elegant programming language lisp. It’s pretty deec out of the box but you can get realllly dirty if you start modifying it to work exactly how you want it to. Customizing your emacs is like building a ship for your pirates. Those guys were pretty vicious on their own but if you give them a ship that’s set up right they are just a fearsome, agile juggernaut.
I gotta respect vi though, it’s a f*cking ninja. It’s compact, decisive, silent, super-quick, and it comes with everything it needs to kill effectively. And just like emacs that shit is *everywhere*, you’ll find it on any non-windows system you have to administrate.
So I steer clear of the debate because a) pirates and ninjas are both bad-ass; I’m pretty sure it comes down to personal preference at that point, b) I don’t know enough about vi to critique it, c) I just don’t give a shit, and neither does anyone outside of nerdworld USA. I also strongly believe that if you couldn’t be equally effective using either the debate would not still be going on unresolved.
With regards to the IDEs I’m distrustful because, while they have a shallower learning curve, it’s sometimes hard or impossible to perform really complex, customized tasks on them because they are automated and rigidly modular. As my Grandfather always said “if it says ‘automatic’, that just means you won’t be able to fix it when it breaks” before he passed away in a tragic handbrake accident. I’m kidding about the accident.
The real deal-breaker for me though is that if you rely on an IDE to do your work, you’ll be unable to function on a remote server without remote desktop and the IDE installed on there. And trust me the situation where you need to edit a program’s code through the shell will present itself sooner than later and you’ll be shit out of the luck if your skills are dependent on an IDE.
Bottom line is, you’re not going to use either for two years and be like “wait, shit, this isn’t for me”. So, ya know, might as well just use emacs
Getting emacs if you’re on windows
OK so if you are in a Mac or a Linux shell, you already have emacs, guaranteed. If you’re on windows, you’re likely going to have to install emacs although I forget, maybe it comes with cygwin or something? There’s an easy way to check whether a particular command is available to you in the shell, which. Here is a screenshot of it displaying the locations of the executable files for some of the commands we have used so far, including emacs:

if you’re on windows and your which emacs turned up nothing, you can download emacs here and unzip it and that should have everything you need to set everything up (read the readme). If you run into problems, try googling a couple things (if you get a specific error message, google that message exactly) and/or post a comment and I’ll help you troubleshoot.
Emacs super-basics
OK let’s open a file and I’ll teach you the most important command for any program — how to exit. First though, let’s get a file to use off the internet…it’s hard to get an idea of how emacs works if you’re not using a live file. For this exercise, we’re going to use the front page of Gary’s book site, crushitbook.com
First pick a directory (I’m going to use my home directory ~, but you can use whatever you want) and cd into there. Then we’re going to pull the HTML file from the interwebs, using a cool command that lets you do that called curl. I’ll show you some dirrrrrrrty curl tricks when we’re installing MySQL (check your gmail from command line, post to twitter from command line, download an entire website’s html without getting blocked) but for now we are going to just use its most common incantation — grabbing a publicly accessible file from the interwebs and saving it to your local filesystem, using curl

Now you see crushit.html has been downloaded to whatever directory you are in. I chose to name it cool.html. Now, let’s open it with emacs

once you’re in emacs, you’ll be available to move around and insert text as you normally would, but a lot of the key combinations you are used to won’t work. Most infuriatingly, you won’t know how to exit and get back to the shell. An escape route is the first thing a savvy programmer looks for in any new technology. The key combination to exit emacs is C-x C-c. As you’ll learn in the built in emacs tutorial I’m going to advise you do when you’re done reading this, that translates to “hold down control and press ‘x’, keep control held down and press ‘c’”
Once you do that you should be back in the shell. Now I could go into the basic features of emacs, but emacs itself does a really good job of that, I’m just going to add one more thing that Mac users need to do to make emacs usable: assign ALT as the meta key. To do this just go into settings on your terminal (command-,), and find and check the option that says ‘use option as meta key’

Now, open up emacs again. You can either open a file explicitly again like you did with cool.html or just type emacs to start the editor. Once you’re in there, enter C-h t which translates to “hold down control and press ‘h’, then release control and press ‘t’”. This will pull up a tutorial screen that will guide you the rest of the way. I know I’ve taken up a lot of your time already and you probably want to quit, but if you’re serious about this you really should go through the tutorial now. It makes every real task you’ll have to do that much easier.

Once you’re done (or if you didn’t care to go through it for now), you can refer to these two cheatsheets I recently made for my B.B. Caleb for some of the most useful/used commands. And yes, that is my real handwriting.


Thanks for stopping by San Diego, you stay classy and I’ll see you next week when you learn how to install MySQL from source, probably.











