Programming from Scratch (Terminal)

Part 2 of this series is over here.

So you're learning to program. Cool. A lot of places start with "languages," but the best tool to get a grip on first is the terminal.

The short answer to why is that your computer is made of files and programs. The terminal exposes this fact and gives you a wealth of tools to exploit it. It can be easy to write off the terminal as a weird and difficult way to explore your filesystem. At first, and armed only with the commands we have here, that is more or less true. However, there are many more commands than you'll see here. That means many more ways to manipulate your filesystem (and computer in general).

Another thing to consider is that denying this view of the computer (in contrast to using GUIs to manipulate the file system) necessarily creates distance and reliance on someone else's determinations of what you should be able to do. Beyond that, the terminal is a more capable interface, not in the sense that you will initially be able to do more, but in the sense that it contains more capabilities. Those are capabilities to discover, experiment with, and eventually lead you to doing more on the command line than you would have been able to do through a GUI based file explorer.

With that out of the way, on to the what after one final assumption: This info is for macs. If you're using a linux-based machine, this will probably work fine. On a windows machine, you might want to look into Cygwin.

Terminal anatomy and definitions

These definitions aren't precise, but they are fine as placeholders.

  • Terminal: The program/application that you can run commands on.
  • (Command) Prompt: The characters to the left of the cursor.
  • Cursor: The (sometimes blinking) indicator of where your typing will end up.
  • Command Line: This indicates the prompt/cursor area, but
  • Shell/Console: This is the interface of the terminal application.
  • Machine: This is what programmers call computers 94% of the time or so.
  • GUI: Graphical User Interface, as opposed to text interface, as in, the interface you mostly use in the terminal
  • Filesystem: A consideration of your machine as containing files and directories.
  • Directory: A "folder" of files and other folders

Places

current directory

.

parent directory

..

root directory

/

home directory

~

The commands

These are (often tiny and specialized) programs (aka. "scripts" or "commands") to run from the terminal. There are thousands(probably?) more. Sometimes they're also called "operators" when they have a very tiny role and don't happen at the beginning of the line.

Making that distinction isn't that important at first. These are the very basic commands you need to do some interesting things. The important thing to know is that there are a billion more commands/scripts/programs like this and you can make up your own.

Directory content listing

ls

Change directory

cd

Present working directory

pwd

Open a file with unspecified program

open
``` (xdg-open in some linux varieties)

This is like double-clicking in finder.
## Find text in a file

grep

## Manual pages

man

##Count words

wc

##Count lines

wc -l

## Take output from another program

|

## Create a file

touch

## See what you did

history

## Remove a file

rm


# OS Shortcuts (Mac)

These are some of the more important ones that aren't super obvious. It assumes that for your chosen OS, you have copy/cut/paste down (respectively ctrl (on windows) or cmd (on OSX) c, x, or v). Also cmd-s or ctrl-s to save should be learned fairly early. 

## Spotlight

cmd-space


##Switch applications

cmd-tab

##Switch instances of application

cmd-tilde (~)



## Preferences

cmd-,


##New Tab

cmd-t


##Close Tab

cmd-w


##Open last closed tab

cmd-shift-t


##Quit program

cmd-q


##New instance of program

cmd-n


## Tab to left

cmd-shift-[

## Tab to right

cmd-shift-]

## Tab #x

cmd-x (where x is the tab number)

## back (browser)

delete
cmd-[

## forward (browser)

cmd-]

## Search

cmd-f


# OS Shortcuts (Windows)

In general, many commands will be the same on windows as on mac. The ctrl and cmd are roughly flipped in function with ctrl being the primary shortcut key on windows, mac's secondary ctrl commands are, more than anything, utilized in a similar way to Emacs's "Readline" interface, which for most Mac programs, is most useful for cursor motions. Also, on a mac the alt/option key tends to support that interface as well as allowing entry of less common characters.

Windows uses ctrl as the primary shortcut key. The windows key is used for large scale coordination between applications and the desktop. The alt key is used mostly for step-through menu manipulation and direct shortcuts. One especially important one is alt-f4, which closes programs.

## Start Menu

windows key


##Switch applications and instances

alt-tab

## Switch from taskbar

windows-t



##New Tab

ctrl-t


##Close Tab

ctrl-w


##Open last closed tab

ctrl-shift-t


##Quit program

alt-f4


##New instance of program

ctrl-n


## Tab to left

ctrl-shift-tab

or

ctrl-PgUp


## Tab to right

ctrl-tab

or

ctrl-PgDn

## Tab #x

ctrl-x (where x is the tab number)

## back (browser)

backspace
alt-left

## forward (browser)

alt-right

## Search

ctrl-f


# Terminal Shortcuts
These are not necessarily specific to the terminal, but that's where they are the most immediately useful and available.

Before working with these, it's recommended that you jettison your caps lock key. Add another ctrl key instead. On a mac this is very simple. Just go to system preferences, keyboard, modifier keys, and change the dropdown there. You might need to hit save/ok.

On a windows machine, to remap capslock, you'll need to adjust the registry manually, or use some packaged solution like [sharpkeys](https://github.com/randyrants/sharpkeys).

One additional change you'll likely want to make is downloading [Cygwin](https://cygwin.com/) rather than using power shell.

#down

ctrl-n

#up

ctrl-p

#backward (left)

ctrl-b

#forward (right)

ctrl-f

#reverse search history

ctrl-r

#forward search history

ctrl-s

##Go to the beginning of the line

ctrl-a

##Go to the end of the line

ctrl-e

##go to last executed command

up key


## Autocomplete

tab key



Part 2 of this series is [over here](http://blog.evanburchard.com/programming-from-scratch-2-files-and-editing/).