A Fledgling Nerd's Guide To

The Command Line

This is the second part of a non-nerd's guide to the command line. You'll be definitively intermediate by the finish. To get started, go that way. →

Editing Files – nano

Looking at contents of files is nice, but you can also edit them right from the command line. The simplest way is arguably nano. Go ahead and save this file to your current working directory and

$ nano my-father-moved-through-dooms-of-love

You can move the cursor around in nano with the arrow keys. nano also has a bunch of shortcuts, which are all listed at the bottom. This way to learn what they mean. ↓

Nano Shortcuts (nanocuts?)

^ means "Control".

"WriteOut" means "Write the contents of this file Out to disc", which really just means "save". In the crusty world of shell, "write" is a common synonym of "save".

So to save the file after editing, hit ^O, that is, Control-O.

To exit, ^X (Control-X).

PS: "nanocuts" would make a good band name
PPS: So would "Crusty World of Shell"

Editing Files – vim

vim is weird & hard to learn, but a lot of people love it a lot. You'll inevitably run into it on your adventures, so it'll be good to know some basics.

So let's open it up.

$ vim my-father-moved-through-dooms-of-love

Ok, there's a lot to say; right this way please. ↓

One Mode to Rule Them All

Vim has modes. Not moods; modes. Maybe it's moody, too, though. idk

Vim starts out in "normal" mode. This is where vim pros spend most of their time. But a funny thing about Normal mode: you can't type anything in it.

Yes! Vim is an editor that, by default, does not let you type text! And though that sounds crazy, it turns out to be delightful, empowering, out-of-the-box thinking.

Find out why ↓

Lazy Scrollers

Vim pros, being good lazy coders, don't want to move their hands the whole painful way to the arrow keys. Have you ever heard of Repetitive Strain Injury? No thank you! To learn The Vim Way, stop reaching and try:

  • j to move down
  • k to move up
  • h to move left
  • l to move right

Aaahhhh… All right on home row! Stop moving those hands! For more vim goodness, right this way ↓

The Most Important Command

u to undo.

Messed something up? Make sure you're in normal mode, then type u. Phew!

To redo, type ^r (that's control (not command) and r. r by itself was already taken!)

Manipulating Text

Even though j and k move a cursor rather than type "j" or "k", Normal Mode is great for manipulating text. Try:

  • dd to delete the current line
  • D to delete to the end of the current line
  • yy to yank (copy) the current line
  • p to paste whatever you last deleted or yanked
  • >> & << to indent & outdent the current line
  • 6dd, 5D, 4yy, 3>>, & 2p – guess. Try them.
  • . to redo the last text-changing command

But what about, you know, actually typing? ↓

Inserting Text

To insert text, you need to put vim into Insert Mode. Once you're done typing, hit escape (top left of the keyboard) to go back to Normal Mode asap. Try:

  • i & I to insert before cursor/line
  • a & A to append after cursor/line
  • o & O to open a new line of text – try it!
  • C to change to the end of the line
  • s & S: substitute (replace) current character/line
  • . same thing as before. Worth mentioning twice.

How to save all these beautiful edits, though? ↓

Colon Commands (: not 💩)

Vim has a whole "command mode" that isn't really a mode. While in Normal Mode, you can type :, followed by a command. For example:

  • :w will save (because "write" means "save")
  • :q will quit vim
  • :wq, :x, or (even lazier) ZZ will save and quit
  • :!mkdir hey will run mkdir hey in your shell – you can run any shell command right from vim this way

Wee! You know so much vim now!
But how to remember it all? ↓

Cheater Cheater Pumpkin Eater 🎃

Vim is best learned in little chunks. If you memorize the commands you just learned, you'll already be as fast (or faster) with vim as with your current editor.

So bookmark this page, and keep coming back until you don't need to. But better yet, bookmark this cheat sheet, and refer to it constantly while you use vim. After a couple weeks, you won't need it anymore.

And one last thing. We all like pretty things. Let's make vim prettier. ↓

It's so ugly 👻

By default, yes. Vim is ugly. But with a configuration file, it too can have colors and remember indentation levels.

Save this file into your home directory as .vimrc. It's a simple file—open it (with vim!) and you could definitely figure out what it's doing. Each line of it is actually something you could type in a colon command (try it out!).

If you want to make it even prettier, check out these dotfiles. Using plugins, they add nice things like a file navigator sidebar, quick file finder, and super-fast folder-wide search.

Flame Wars 🔥

Some programmers get really territorial and religious about their editor of choice. They make gang signs for vim and emacs (another popular editor). It's silly.

comic about editors

Customize Your Shell 🐌

Vim uses a ~/.vimrc file for configuration. Bash uses a ~/.bashrc.

rc stands for "Runtime Control", which doesn't shed a whole lot of light. Just remember that rc files are configuration files.

Let's customize it! Open ~/.bashrc using vim, and head thataway ↓

alias yourself a shortcut

Let's pretend you have to change to your desktop all the time. You're sick of typing this:

cd ~/Desktop

You're in luck! You can add a simple alias to your .bashrc! Add this:

alias d="cd ~/Desktop"

Now try it out. Save and exit vim (remember how?) and enter d. Didn't work? ↓

I like the way you source it, source it 💃🏼

Before changes to your .bashrc take effect, you need to source the file.

Your bashrc is automatically sourced when you open a new shell. So open one! (Press ⌘T for a new tab.) And now press d. Coool. 😎

But now head back to that original shell. Enter . ~/.bashrc (shorthand for source ~/.bashrc). Now enter d. Sweet!

Ok, one more thing ↓

Go Hog Wild 🐖

Here's a nice introductory .bashrc file for you. Go ahead and download it, saving to your home folder as ".bashrc" (with the dot!).

Open it with vim and play around!

Wondering what else you can do with a bashrc? Search Github for lots of other ideas!

Pretty Prompt 💅🏼

Look through the .bashrc you downloaded for instructions on how to make your shell look like this:

a shell with a pretty and minimalist prompt

Know Your Root

You're not allowed to do certain things on your computer, because you could cause serious damage. You could literally erase your whole hard drive by running something like rm -rf / (don't do this!!). Since no one wants to accidentally destroy things, you have to do extra work to change system files. You need root access. You need to be a super user. An administrator.

This works differently on Windows than anything else. This way for instructions on any system. ↓

Live Dangerously – Unix

On most *nix-based systems these days (unix, linux, macOS), you're not allowed to just run around as a super user because that's scary. Instead, you prefix specific commands with sudo:

$ cd /
$ ls
$ mkdir bahaha
$ sudo mkdir bahaha

When you type in your password, you won't see it. You won't even see splats. That's intentional, just type your password into the void & hit Enter.

/ is the root directory, which is where your whole hard drive's filesystem starts. When you cd .. here, you stay here. You're not allowed to change things in here unless you sudo, which means Super User DO.

Living Dangerously on Windows

On Windows, you'll need to find the Git-Bash icon either on your Desktop or in the Windows Menu (if you pinned it to the taskbar, this won't work there.) Right-click the icon and select "Run as Administrator". Now you can live as dangerously as you want! For example, you can now:

$ cd /c
$ mkdir woohoo
$ rm -r woohoo

It's Like This

"Make me a sandwich." "What? Make it yourself." "Sudo make me a sandwich." "OK."

By the way, you might want to rm -r bahaha now.

Getting Other Unix Programs

Sometimes the inbuilt stuff just isn't enough! If you want more, you'll need a package manager.

$ cowsay package managers rock!
  _______________________
 < package managers rock! >
  -----------------------
          \   ^__^
           \  (oo)\_______
              (__)\       )\/\
                  ||----w |
                  ||     ||

Yes! An ascii cow! This way to get your own. ↓

Getting Cowsay and other fun things on macOS

The preferred package manager for your Mac running macOS these days is Homebrew. An older way was MacPorts, so you might still hear people mention that now & then. You can follow the link to Homebrew to see how to install it, but once you do, go ahead and

$ brew install cowsay
$ cowsay Hooray!

Note that Homebrew may whine about not having Xcode (or a compiler). Luckily, for cowsay, you don't need it. But for some things you might, so feel free to download Xcode from the Mac App Store (you only need the command line tools for Xcode, but to just get those you need to sign up as an Apple developer. But that may be faster, since Xcode is a huuuge install.)

Getting Cowsay and other fun things on Linux

If you're using Linux, then you already have a package manager that you can use. On Ubuntu, you have two options, apt-get or aptitude. aptitude seems to be preferred by some people because it's better at uninstalling things. So:

$ sudo aptitude install cowsay
$ cowsay Hooray!

If you're using a different version of Linux, then instead of aptitude you might have yum or something. Just search the web for what package manager to use with your flavor of Linux.

Getting Cowsay and other fun things on Windows

No idea. If you know, please tell me.

Read The Effing Manual 😡

Now and then, when n00bs ask for help on forums, the crotchety enlightened will respond with a curt "rtfm". What manual??

Usually, every unix program comes with a manual (also called a "man page"). To read it, just do this with any command:

$ man ls

This may open in less. Remember less?

Go ahead and check out the manual for some of the programs you've learned about (less, cowsay, etc).

Pipes

Look at the slash key on your keyboard that's above the Enter key (that's the backslash, by the way; the one on the ? key is just "slash"). The vertical line above the backslash is called the "pipe" character.

The philosophy of unix is that programs should do one very simple thing, and do it well. Then you can use pipes to hook multiple programs together. Try this:

$ ls ~ | less

You piped the results of ls into less! There are other kinds of pipes, too. ↓

A Series Of Pipes

Try this:

$ echo "So what?"

Indeed, so what? echo just spits out what you tell it to. But with pipes, it gets cooler.

$ echo "Here's something" > tmp.txt && less tmp.txt

The > pipe totally overwrites a file. Try this now:

$ echo "& more." >> tmp.txt && less tmp.txt

The >> pipe appends to a file without destroying what was already there.

Blanking Out Files

There's an interesting twist on the > pipe: it doesn't need anything before it. Try this:

$ > tmp.txt

You just piped nothing into tmp.txt. You overwrote all of its contents with nothing. Open it up (cat it, or more it, or whatever-you-like it) and you'll see that it's now empty.

I Quit

To force quit a process that seems to be stuck, just hit control c (not C, macOS users!) Let's try it. Copy & paste this command:

$ I=0; while [ $I -lt 100 ]; do \
    echo $I >> count.txt && let I=I+1 && sleep 1; \
  done && less count.txt

Long command! But now you're in familiar-by-now less, looking at a file called count.txt. It's a boring file right now. Just a 0. But wait, there's more! Press shift f to follow the file. omg, counting!

But now less is stuck! Aha. Press control c to stop following the file. Then you can q to quit, just like normal.

Awesome

You know a lot about the command line.

Go boss your computer around.

But I want to boss around a different computer 🖥

Good for you!

Go learn about ssh (secure shell)

Digital Ocean has a great guide to setting up your very own server. Once it's set up, you can ssh into it. Then you'll have command-line access to a whole different computer. A computer in the cloud! All of the commands you just learned will still work on it.

You're ready to rule the world.