# Intro to the Command Prompt [gregorymcintyre.com](https://gregorymcintyre.com) 2017 ==== ## The Command Prompt - Purpose - Anatomy - Get unstuck - Get happier ==== ## Purpose What do I use the command prompt for? Let's find out! ```small cut -f1,2 -d ' ' ~/.bash_history \ | sort | uniq -c | sort -nr | head -n 30 ``` ==== ## Purpose Honest answer time: ```small git status ls git add git diff git grep git commit git push cd .. git log git checkout cd npm i git pull git rebase git stash git reset ``` ==== ## Purpose Mostly: Git ```small git status git add git diff git grep git commit git push git log git checkout git pull git rebase git stash git reset ``` ==== ## Purpose Frequently: Rails tells me to run a command ``` rails new myapp rails g model Blog title:string rails console rails db rails s rails routes rails db:setup rails db:migrate ``` ==== ## Purpose (Same, but other programming languages) ``` node . npm start npm watch gulp webpack yo ``` ==== ## Purpose Less frequently: Install language packages ``` bundle init bundle upgrade gem install npm init npm install ``` ==== ## Purpose Less frequently: Install applications ``` brew install apt-get install yum install ``` ==== ## Purpose Rarely: Big complex powerful commands that take *days* to research and get just right ==== ## Purpose ```small git log $(git merge-base master branch)..branch cut -f1,2 -d ' ' ~/.bash_history \ | sort | uniq -c | sort -nr | head -n 30 wget -mk http://www.stuff.to/read/on/the/train/ rsync -rvP myproject/ username@myhost/myproject.com/ pg_dump --clean -Z 9 app_master | \ ssh user@apphost 'zcat | psql -h localhost -U app appdb' ``` ==== ## The Command Prompt - ~~Purpose~~ - Anatomy - Get unstuck - Get happier ==== <!-- .slide: data-transition="fade-out" --> <img class="borderless" src="term-running-middleman.png" alt="Terminal running middleman"> ==== <!-- .slide: data-transition="fade-in" --> <img class="borderless" src="term-running-middelman-hl.png" alt="Terminal running middleman with highlights"> ==== ## Terminal Emulator - AKA terminal or just "term" - Is itself a graphical application - Runs programs that use text for input & output - By default, runs a **shell** ==== ## Terminal Emulators - /Applications/Utilities/Terminal.app - /Applications/iTerm.app - cmd.exe ==== ## Emulator? <img class="borderless" src="bios.jpg" alt="BIOS"> ==== ## Console? - Named after the piece of furniture - Implies a keyboard and a screen of text - Imagine wood veneer and glowing orange text ==== ## Shell? - A program that runs other programs (like rails!) - Can think of those programs as being "inside" the "shell" program ==== ## Shell? - Print prompt - Wait for you to type (e.g. `rails s`, return) - Run command you specified - Print output until command exits - Print prompt - ... ==== ## Shells - sh (Bourne shell) - bash (improved Bourne**-Again** shell) - zsh (from author Zhong Shao) - cmd.exe (old Windows) - PowerShell.exe (modern Windows) ==== ## Command Prompt? - The bit of text that a **shell** prints before it waits for your input - Lots of variety ``` greg@mycomputer ~$ mycomputer% C:\> ``` ==== ## Commands ``` git log --stat 'git' <-- executable program 'log' <-- argument 1 '--stat' <-- argument 2 ``` ==== ## Pipe Send the output of one program into another `cut -f1,2 -d ' ' ~/.bash_history | sort | uniq -c | sort -nr | head -n 30` ==== ## Redirect Send the output of a program into a file `rails db:migrate > rails-migrate.log` ==== ## The Command Prompt - ~~Purpose~~ - ~~Anatomy~~ - Get unstuck - Get happier ==== ### Common problems amongst beginners ==== ## Dealing with spaces in file names ``` $ cd My Documents cd: My: No such file or directory $ cd 'My Documents' $ cd "My Documents" $ cd My\ Documents ``` ==== **I ran `rails s` and now I don't have a prompt anymore** - Prompt won't come back until `rails` exits - Open up a new terminal (recommended), or - Press `Control-C` to ask Rails to stop ==== **I ran `irb/pry/node` and I can't get out!** - exit, quit, q, mashing escape, nothing works - `Control-D` is the most universal shortcut to exit any prompt ==== **I press keys and nothing happens or garbled text gets printed** - Some programs move the cursor or change text colour - Can leave your terminal in a broken state - Quit your terminal application and start a new one, it's OK ==== **Where do I get help?** - Read the manual (man = manual, spacebar for next page, q to quit) `man wget` - Ask a program to print help for you `rails --help` - Universal solution: - Google "wget command" or "rails command" ==== <img src="xkcd-flowchart.png" alt="XKCD Flowchart"> ==== ## The Command Prompt - ~~Purpose~~ - ~~Anatomy~~ - ~~Get unstuck~~ - Get happier ==== ## Nicer Prompts ![Custom prompt](custom-prompt.png) ==== ## Aliases ``` echo "alias gs='git status'" >> ~/.bashrc $ gs ``` ==== ## Colour ``` echo "alias ls='ls -ohG'" >> ~/.bashrc ``` ![Ls Colour](ls-color.png) ==== ## Colour ALL the programs \o/ `git config --global color.ui true` ![Git Colour](git-color.png) ==== ### Don't panic - Focus on these commands ```small cd cd .. ls ``` ==== ### Don't panic - Keys: Up, Down, Tab ```small $ atom ind<Tab> $ atom index.html $ <Up><Up><Up><Down><Down> ``` ==== ## The Command Prompt - ~~Purpose~~ - ~~Anatomy~~ - ~~Get unstuck~~ - ~~Get happier~~ ==== Get lots of work done and feel good <img class="borderless" src="nyan-cat-deal-with-it.gif" alt="Nyan cat deals with it"> ==== [gregorymcintyre.com/slides/intro-command-prompt/](https://gregorymcintyre.com/slides/intro-command-prompt/#/)