A little taste of programming: the terminal and vim

The Stack Overflow blog published a post about vim the other day. I want to share it with my non-programmer friends:

Stack Overflow: Helping One Million Developers Exit Vim

I’m going to try to give some context to help non-programmers appreciate it.

What is vim?

vim is a text editor that predates modern user interfaces. It’s also usually the default text editor for your computer’s terminal/command promptIf you’ve ever taken a computer science course you may have typed commands into the terminal before.

macOS terminal (the first result when you search “terminal” on Google Images)

It was a different time

Long before we had Windows’ windows, toolbars, and buttons, the command line reigned supreme.

Everything you did with a computer got accomplished by typing keywords into a terminal with exactly the right syntax. What, that sounds tedious? Certainly less tedious than doing long division on thousands of numerical values. Computers were–and underneath it all, still are–just fancy calculators.

Here are a couple pseudocode examples of simple commands like get().

List all users from account database:

$ accounts.users.get_all()

Get user whose id is 201:

$ accounts.users.get(id=201)

List users with accounts created before 2014 (although the year 2014 would probably be represented differently from what I wrote here):

$ for user in accounts.users {
  ..  if (user.creation_date.year < 2014) { 
  ..      user.get();
  ..  }
  ..}

If there was something the computer can probably do but you didn’t know how to type the command? Then you’d spend the day or week poring over documentation, filtering through dozens of explanations of keywords you didn’t need before finding something you can use. There was no Google back then to answer your questions. No Stack Overflow.

Using vim/vi

The following command will launch vim from the command line and open an empty file saved as new.txt.

$ vim new.txt

The lack of individual windows meant that when you’d enter the command, vim would just take the place of the terminal within the same window.

With vim open you’d type up your entire document, save, and exit back into the terminal. Back then, everyone was a hipster and they were all mono-tasking, and there was no auto-save.

The whole process

So you’re in your terminal and you want to write up some text and save it.

When you execute $ vim new.txt, vim opens in what’s called Command Mode. Here’s a screenshot of vim in Command Mode:

Screenshot 2017-05-28 17.24.03

The other commonly used mode in vim is Insertion Mode. If you’re in Command Mode hitting i on your keyboard will switch you into Insertion Mode. Now you can type up your document as if you were using a normal text editor.

Again the steps to open vim and start writing are:

  1. $ vim new.txt
  2. Hit i
  3. Write stuff
Mac OSX Terminal with vim open and "Now I can type" written
See where it says — INSERT — at the bottom

Once you’re done typing you’re gonna want to save and quit. You can’t do this while inside of Insertion Mode.

How to save and quit vim from Insertion Mode:

  1. Hit the ESC key to switch back into Command Mode
  2. Type :wq and hit the Enter / Return key to send the command
  3. Now you’re back in the terminal and your file was successfully saved as new.txt

When you type in Command Mode your commands appear in the status bar at the bottom of vim. (Note: I didn’t say “at the bottom of the window” because we’re not using windows, it’s all inside the terminal)

Mac OSX Terminal with vim open in command mode
See the bottom where it says ‘:wq’

Help I’m trapped

Can you see why a million people have needed help with exiting a simple text editor? (Quote below edited for readability).

I think there are two reasons it’s easy to forget how to exit Vim:

  • developers are often dropped into Vim from a git command or another situation where they didn’t expect to be, and
  • they run into it infrequently enough to forget how they solved it last time.

Why do we put up with this?

This is one of many situations where programmers sound like masochists. Why do we continue to use this weird program that basically requires a cheat code to get out of?

For the most part, we just get used to it. Nearly all programming requires the use of some kind of terminal. It’s not so bad once you learn all the arbitrary rules.

Really, programming at its core is just trying to build stuff with a computer’s arbitrary rules. We signed up for this.

vim is far from the hard part about programming though. It’s just an unfortunate hazing ritual we have to go through.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s