Skip to content

Instantly share code, notes, and snippets.

@NickTomlin
Created October 4, 2012 17:02
Show Gist options
  • Save NickTomlin/3834959 to your computer and use it in GitHub Desktop.
Save NickTomlin/3834959 to your computer and use it in GitHub Desktop.
Terminal Cheatsheet

Navigating / Manipulating Text (emacs style terminal) [3]

Ctrl + a - Jump to the start of the line

Ctrl + b - Move back a char

Ctrl + c - Terminate the command

Ctrl + d - Delete from under the cursor

Ctrl + e - Jump to the end of the line

Ctrl + f - Move forward a char

Ctrl + k - Delete to EOL

Ctrl + l - Clear the screen

Ctrl + r - Search the history backwards

Ctrl + R - Search the history backwards with multi occurrence

Ctrl + t - Transpose the current char with the previous

Ctrl + u - Delete backward from cursor

Ctrl + w - Delete backward a word

Ctrl + xx - Move between EOL and current cursor position

Ctrl + x @ - Show possible hostname completions

Ctrl + z - Suspend/ Stop the command

Ctrl + x; Ctrl + e - Edit line into your favorite editor

Alt + < - Move to the first line in the history

Alt + > - Move to the last line in the history

Alt + ? - Show current completion list

Alt + * - Insert all possible completions

Alt + / - Attempt to complete filename

Alt + . - Yank last argument to previous command

Alt + b - Move backward

Alt + c - Capitalize the word

Alt + d - Delete word

Alt + f - Move forward

Alt + l - Make word lowercase

Alt + n - Search the history forwards non-incremental

Alt + p - Search the history backwards non-incremental

Alt + r - Recall command

Alt + t - Transpose the current word with the previous

Alt + u - Make word uppercase

Alt + back-space - Delete backward from cursor

Tab Commands

(Here "2T" means Press TAB twice)

$ 2T - All available commands(common)

$ (string)2T - All available commands starting with (string)

$ /2T - Entire directory structure including Hidden one

$ (dir)2T - Only Sub Dirs inside (dir) including Hidden one

$ *2T - Only Sub Dirs inside without Hidden one

$ ~2T - All Present Users on system from "/etc/passwd"

$ $2T - All Sys variables

$ @2T - Entries from "/etc/hosts"

$ =2T - Output like ls or dir

Because I use vim or vim clones like Sublime Text 2's vintage mode, I prefer to use the console in vi mode, rather than the default emacs.

A lot of the information here is encapsulated in the ever knowledagble Peter Krumnis' bash vi editing mode cheat sheet. I'm going to repeat bits and pieces here and there because that's how I learn.

Note

This contains commands shamelessly stolen from many authors. I will try to list all of them at the end of the file, but if I've missed someone, let me know.

Legend

  • UC/Use Case : a practical/frequently used example of the command in action (I find it easier to add commands into my daily workflow if I have an example that uses them in something I often do)

The Prompt


Navigation

cd - Go back to your $HOME directory (~) cd - Go back to the previous directory you were in

Master level navigation with pushd and popd

Still learning this, good introhere. I now use z, and dirs to navigate around. It's a better fit for me.

Command history

Working with your previous command

!! - Last command !foo - Run most recent command starting with 'foo...' (ex. !ps, !mysqladmin)

!foo:p - Print command that !foo would run, and add it as the latest to

!$ - Last 'word' of last command ('/path/to/file' in the command 'ls -lAFh /path/to/file', '-uroot' in 'mysql -uroot')

!$:p - Print word that !$ would substitute

!^ Print first word of last command

!* - All but first word of last command ('-lAFh /path/to/file' in the command 'ls -lAFh /path/to/file', '-uroot' in 'mysql -uroot')

  • UC source your profile after editing it:
  • vim ~/.bash_profile
  • source !*

!:p - Print words that ! would substitute

!:t filter out the last word in a long sequence. Great for URLssource

Grabbing the nth word of last command 6

Given pip install lxml

!:1 grabs the 2nd (bash works from a zero index) command so install in our example

!:0-1 grabs from the 1st to the 2nd comand so pip install in our example

Replacing text

^foo^bar - Replace 'foo' in last command with 'bar', print the result, then run. ('mysqladmni -uroot', run '^ni^in', results in 'mysqladmin -uroot')

Working with History [2]

note you can combine the commands above with past commands. For example: rm !5429$ will run rm on the last word of command #5429 history - Print all previous commands you've run (use history | grep "ssh" to search for command and make your life easier)

!-4 - run the command that is 4 commands back from your last command

!2 - Run command with the id of 2.

!'string' - run the last command that begins with 'string'

Executables


Find out Where a program is located

which subl - Outputs path where executable subl is located e.g. usr/local/bin

Find where a program is located, and cd to that directory

We are going to use the $() evaluator to nest several bash expressions along with dirname which gives the directory path of an executable.

cd $(dirname $(which drush))

cd `dirname $(which subl)` change the current directory to the path of subl e.g. (cd /usr/local/bin) [4]

Files


Quick renaming files with brace expansion link

``mv very/long/path/to/filename.{old,new}

Batch renaming file extensions with rename

Note: rename is not installed by default on some systems (Mac Os included) user your favorite package manager to install it. Homebrew users can just brew install rename.

The general syntax is:

rename 's/search_for_string/replace_string_with_this/' files

UC: Change the file extension of all .html files to .md files, while preserving the names

Given the following directory:

regular-old-file1.html regular-old-file2.html regular-old-file3.html regular-old-file4.html regular-old-file5.html regular-old-file6.html

The following code:

rename 's/.html/.md/' *.html

outputs:

regular-old-file1.md regular-old-file2.md regular-old-file3.md regular-old-file4.md regular-old-file5.md regular-old-file6.md

This is a pretty simple use case, but I find that I frequently have to change file extensions/names on large groups of files. This is a lot better than manually using mv everything.

Find the type of a file without an extension:

file bar will output something like bar gzip compressed data, from Unix, last modified: Fri Mar 1 08:33:16 2013

Customizing


.bash_profile = sourced by login shell,

.bashrc = sourced by all shells,

.bash_aliases = should be sourced by .bashrc/.bash_profile

Making changes

Any modifications you make to your profile will only show up after you:

  • A: open a new shell window OR
  • B: source your profile with source ~/.bash_profile (preffered IMO)

Use bash in "vi" editing mode

Add the following to your .bash_profile set -o vi

  • Be warned that this takes some getting used to

Avoid duplicates in your history:

export HISTCONTROL=ignoredups

SED


Grep and Sed files

search the current directory for a string, and replace it with another (note, probably better to use perl's built -p -i -e in search, see recipes for an example) grep -lir 'id="left"' * | xargs sed -ie 's/id="left"/id="left" class="sidebar"/'

Redirecting, Piping, and passing commands

Redirecting

You can redirect the out put of a command into a file, or the contents of a file into a command with the < and > characters, respectively.

Writing output of command to file with >

ls > files.txt writes the output of hte ls command to the file files.txt echo '# This is a new markdown file' > memoir.md adds the echoed text to a new markdown file (overwriting any existing file with that name). echo "the end." >> memoir.md adds "the end." to the end of the memoir.md file.

Piping

To pass the output of one command to another, connect them with the | character. E.g. ls -C | wc - w Returns the number of files in your current directory.

  • to redirect output, and display on screen use the tee command *

Executing commands

You can use the output of commands within other commands by wrapping the command in a `character.

e.g. vim `which drush` passes the outputh of the which command (in this case a path: /usr/local/bin/drush) to vim, which then opens the file.

  • to nest multiple commands, use $() Explanation* cd `dirname $(which subl)` change the current directory to the path of subl e.g. (cd /usr/local/bin) [4]

Interactive Calculations with BC

bc to enter basic calculations mode (there is a lot of stuff you can do here)

Thanks to

Curl is the swiss army knife of http requests -- actually, let's be serious: it's the optimus prime of http requests. As such it deserves it's own section (although it may show up elsewhere on ocassion).

Help

I've found curl --manual to be a lot more helpful than the linux man page (man curl). Feel free to | it to your favorite editor.

Flags

-I Request headers -g GET (default)

curl -I -H 'Accept-Encoding: gzip,deflate' http://nick-tomlin.com

Remotes (pushing/pulling)

Pull down references to all remote branches

git pull --all

Branches

View the contents of a file in another branch, without switching branches source

git show master:./sass/_site-default.scss | subl or git show master:./sass/_site-default.scss | vim - (the - tells vim to use stdin).

master in the above can be a reference to any legitimate commit, so you can use a hash/tag as well.

Staging / Commmiting

Add all deleted files to the staging area

git add -u

Add only deleted files to staging area

git ls-files --deleted -z | xargs -0 git rm source

Stash untracked files

git stash -u source Bad things have happened...

Merges

Show diff for "staged" changes only

git diff --cached

Open unmerged files in an editor

This assumes that your HEAD is clean.

git diff --name-only | xargs subl
vim -p $(git --no-pager diff --name-only)

Merge two non-sequential commits into eachother

source

  1. Figure out which commits you want : )
  2. git rebase -i ^
  3. Reorder reorder the commits to be next to one another, use squash or fix to merge commits

Cleanup

Use the -e <glob pattern> use git clean -xd but only on files of a certain extension. Source clean, but leave text files: git clean -df -e *.txt

When things go wrong

Counteract the changes in a single commit

  • git rebase -i <commit>~1 source
  • delete the line of the commit that you want to remove
  • Git will rebase (hopefully without errors) (this is applicable to multiple commits as well, provided that the changes don't intersect with a lot of other commits)

Undoing a botched merge

  1. that has not been pushed | source

git reset --hard origin/master makes local reflect master. 2. That has been pushed

coming soon 3. A rebased merge (a feature branche's commits have been added on to masters commit history, so there is no clear commit to go back to) | source

git reflog # find commit before merge (in this case HEAD@{5}) git reset --hard HEAD@{5}

Diff

Submodules

Batch update submodules

Init and Update all submodules in a repo: git submodule foreach git submodule init git submodule foreach git submodule update

Clients

There are a lot of IRC clients out there. Both GUI and CLI. I've played around with several (for mac OS) and my top two are:

Gui

CLI

Getting nickName

Everyone on IRC is identified by a Nick, wether they choose so or not (it usually default to the username on your computer). You can set a nick by using /nick <name>. The only issue with this is that anyone can use your Nick, if they grab it first. To prevent this, you can register a Nick (instructions here).

Talking

It's a convention to preface messages to users with their nick. So, if i'm talking to a using named "foobar" I would chat foobar: that's correct. You can use tab completions in IRSSI to make this faster.

Irssi

This handy guide to windows has some great tips.

Installing scripts/plugins

  1. Create a plugins directory in your ~/.irssi
  2. Create a autorun directory in your ~/.irssi/plugins folder (this scripts will be — you guessed it — automatically run on startup)
  3. Download any of the IRSSI scripts to your plugins folder
  4. To automatically enable these plugins, create a symlink in your plugins folder cd ~/.irssi/plugins/autorun and ln -s ../pluginname.pl .

Removing annoyances

As suggested in the IRSSI tips & tricks, you can use crapbuster to help trim down those annoying join and leave messages.

once crapbuster is installed in you plugins folder (and symlinked to autorun), you can use the following settings (stolen from IRSSI tips and tricks):

/set crapbuster_levels JOINS PARTS QUITS NICKS CLIENTCRAP CRAP MODE TOPICS KICKS

now you can run /crapbuster to remove the offending messages.

A better way:

Manually edit your settings file: Guide here

or enter these options IRSSI, and then use /save to save them.

Networking

DNS lookup with DIG

To see IP that hostname is resolving to:

dig +short www.google.com (leave of the +short to get a more detailed report)

View route to domain with traceroute

Useful for clearing up DNS confusion.

traceroute www.google.com

Random osx shortcuts.

open preferences pane in any GUI application (Or ST2 pref file ^^) cmd + ,

Recipes

Random Useful stuff ^^

Note: if a section grows too large, consider splitting it off into a seperate document surrounding the command/concept

Search Google for terms

From: http://superuser.com/questions/47192/google-search-from-linux-terminal

Add this to your .bash_profile / .zshrc etc.

google() {
    search=""
    echo "$1"
    for term in $*; do
        search="$search%20$term"
    done
    # changing to ``open`` instead of ``xdg-open`` for OSX
    open "http://www.google.com/search?q=$search"
}

Loop through the contents of a file link

# Mac os x, "urls.txt" contains a page url on each line
while read p; do open -a Google\ Chrome $p; done < test.txt

Find recently modified file and move them somewhere

Great for those "oh, I didn't mean to untar to my user directory" moments. Not that I ever have those ^^

Find All jpeg files that have been modified in the past 22 minutes and move them to the /tmp directory find . -name "*.jpg" -mmin -22 -print0 | xargs -0 -I {} mv {} /tmp

Alternately:

find . -name "*.jpg" -mmin -22 -exec mv "{}" /tmp \;

Or, the more ters — but potentially less useful — -delete switch:

find . -name "*.jpg" -mmin -22 -delete

Unpacking that first one:

  1. in the current directory (.)

  2. find all files that start with anything (*) and end with ".jpg"

  3. that have been modified in the past 22 minutes or less (-mmin -22)

  4. and print each on one line followed by the null character(-print0).

  5. Pass that output (|) to xargs,

  6. with instructions to seperate each seperate command with the null character (-0) and use "{}" as a placeholder (-I {}).

  7. Given that placeholder, and those output, mv them to the /tmp directory. Whew!

Side Note: use the -r switch to stop xargs from executing commands if the stdin it was handed by "|" is empty. xargs -0 -I {} -r ...

Alternative:

use find / exec for "safer" execution (potential performance hit) unix.stackexchange

Download a tarred folder and extract the contents of it to a specified directory

curl -L -o nt-dotfiles.tar.gz https://api.github.com/repos/nicktomlin/laptop/tarball | tar --strip-components=1 -x -C `echo $HOME`

Unpacking that:

  1. "curl" following the contents of the url https://api.github.com/repos/nicktomlin/laptop/tarballfollowing symlinks (-L) to a local file -o : nt-dotfiles.tar.gz
  2. pass (|) that local tar to tar stripping the first level of components --strip-components=1 (in this case a containing folder) and extract (-x) them to a destination directory (-C) that is the result of the command (`echo $HOME`) (this could be any valid path)

Simple simple:

Download the tar directory at the given url (following symlinks with -L) and Expand it in the currenty directory curl -L https://api.github.com/repos/nicktomlin/laptop/tarball | tar -x

Find files (by timestamp) in relation to another file with the -newer flag

On the simplest level, if you know your target directory, and have a file around the date http://alvinalexander.com/unix/edu/un010013/

find /Volumes/clients/Online\ Media\ Signals/Project\ Website/Design/ -name "*.psd" -newer /Volumes/clients/Online\ Media\ Signals/Project\ Website/Design/5-online-media-signals-contact-2.psd -print0 | xargs -0 -I {} cp {} ~/Projects/online-media-signals

Grep files for a string, removing dupes

http://serverfault.com/a/291327/152051

Sed

Replacing/Finding/Deleting with Sed:

source (mostly)

-i in place editing (dont' redirect changes to STDOUT) -i".bak" edit in place, but make a backup file with the extension specified in quotes ". Optionally '-i.bak' should work for most systems.

  • Replace a string sed -i.bak 's/foo/bar/g'

  • Delete the 18th line of a file, creating a backup of the file with the extension ".bak": sed -i".bak" '18 d' /path/to/somefile.txt

  • Delete lines containing a string: sed -i".bak" '/foo/d' /path/to/somfile.txt

  • Combine with find to replace a string (thanks tom!): find . -name \*.php -exec sed -i "" -e "s/orgVar:/newVar:/g" {} \;

  • Combine with ack source ack -l orgVar | xargs sed -i "s/orgVar/newVar/g"

Find string, and append afterwards

sed '/<title>/a\
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">' 1.txt

Remove leading zeroes from filename

for FILE in *; do mv $FILE echo $FILE | sed -e 's:^0*::'; done

Append a snippet of code after a line

sed -i.bak -e "100a\\
 drupal_add_js('sites/all/themes/sndev/js/mongoose_cookie.js', 'file');" template.php

Add local SSH key to remote server link

cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> .ssh/authorized_keys'

Find string and replace source

(for a much easier solution see perl example below)

grep -lr -e '<oldword>' * | xargs sed -i 's/<oldword>/<newword>/g'

Find and Repalce with Perl source

perl -i -p -e's/targetString/replaceString/g' *

Or, with backup: perl -i.bak -p -e's/targetString/replaceString/g' *

Images

ImageMagick ()

trim whitespace from an image source convert -trim image.jpg image.jpg (unfortunately I do not know of a way to avoid putting the image name in twice)

For a comparison of regex engine features see here.

General

Javascript

Perl

Python

Syntax

If syntax:

if [[ -e .ssh ]]; then echo "hi"; fi

The [[]] is necessary for shell compatibility. I don't know any more than that right now :)

Numerical comparison:

i=0; if (( i <= 1 )); then echo "smaller or equal"; else echo "bigger"; fi

file check flags:

  • -e: file exists
  • -f: regular file (non directory)
  • -d: directory
  • -s: non-zero file
  • -x: execute permission

Loops

Loops can be run within a script/function as multiple lines:

for i in {1..5};
do
  echo "current number is $i"
done

or as a single line (in a prompt), by separating each line with a semicolon:

for i in {1..5}; do echo "current number is $i"; done

Loops on files:

Let's loop over ever .txt file in the directory, and add "edited by" + the current shell username to the end of each file. Note the use of >> to add to the end of a file, and the grave accent mark ` to run a shell command within a script

for i in *.txt
do
  echo "edited by `whoami`" >> $i
done

Or a single line version:

; for i in *.txt; do; echo "edited by `whoami` " >> $i; done

Control Structures

If

@todo

Check if program exists:

if hash rename 2>\dev\null
  then
    # do stuff
fi

Making it executable

chmod a+x

Check if a file is executable with file

*nix has a built in file command that will give you pertinent details about the file (what architecture it is compiled for and if it is executable or not)

I use ST2 in Vintage mode as my primary editor. This is to reduce the confusion of looking at a vim cheatsheet and realizing ST2 has a slightly different implementation of a feature.

Ex Mode

Vintage is pretty decent to start off with, but to use some the more awesome vim commands, you should install sublime EX (available in package manager or this repo)

Preferences

// start in command mode (and stop accidentally deleting swaths of code!)
  "vintage_start_in_command_mode": true,
// use system clipboard in vintage mode
  "vintage_use_clipboard": true

Differences from vim

Recipes

Multiple Selection

cmd + shift + l is your ticket here.

Edit Multiple Lines

This one is a little tricky, but pretty darn awesome (esp. when dealing with lines of uneven length) when you get the hang of it:

  • Select the lines you want to edit in visual mode
  • cmd + shift + l (enable multiple cursors)
  • v ( twice! - to exit visual mode)
  • i (or any other combination to enter insert mode)

Edit all occurrences of word

Similar to the normal ST2 method, but with a little multiple vim thrown in

  • move cursor over target word
  • ctrl + cmd + g (select all occurences)
  • cmd + shift + l
  • v to exit visual mode

Change the content of all <p>

  • move cursor over target tag <p> perform the steps above c i t to change the content of selected tags

Visual Mode

  • o reverse direction of selection (helpful if you've accidentally missed one line at the opposite end of your selection)

Opening files at a given line number / function from command line link

test.php:240

Open to a function:

test.php@<function name>

Find and Replace (requires EX)

find and delete all blanks spaces :g/^$/d source

Errata (not necessarily vim/vintage related, but helpful)

Get out of the "Do you want to save the changes you made to a new file?" Window without using your mouse:

super + backspace (cmd + delete in OSX)

VIM


Saving and closing

:w write file :q close :wq write and close file :q! close, discarding any unsavd changes

Some time saving shortcuts (in command mode):

ZZ write and close file ZQ close filew without saving

Command line

Open file at a specific line:

foo.txt +200

Pipe command from stdin into vim for editing

- tells vim to look at stdin for input.

ls | vim - opens the results of ls in vim. Mix and match with all your favorite commands ^^

Efficiency

Switching Editing modes

  • Use ctrl + c instead of esc to quickly exit insert mode
    • Do note that ctrl + c immediately cancels whatever action your are doing (esc is graceful in stopping commands), so it may not interact well with certain motions. You can make ctrl + c function like esc by adding the following to your .vimrc: vnoremap <C-c> <Esc>

Moving Around [1]

Locations

  • 0 Beginning of line
  • ^ First character of line (so wonderful for lines with a lot of indentation)
  • $ End of line

Lines

  • 40| move to the 40th "column", handy for moving to themiddle of a line if you have text wrapping at 80 characters

Visual Mode

  • v start highlighting characters
  • V start highlighting lines (I missed this one for so long)

Characters

  • t c move the cursor to the space before next 'c' character after the current cursor
  • T c move the cursor to the space before the nearest'c' character before the current cursor
  • f c move the cursor to the nearest 'f' character before the current cursor
  • F c move the cursor to the nearest 'f' character after the current cursor
  • ; repeat the last find character command
  • , redo the last find character command in the opposite direction

Efficient Deletes

Remember, you are able to combine most vim shortcuts/motions with delete. So d + $ deletes to the end of the line, d 4 wdeletes four words in front of the cursor.

In Command Mode

Words
  • ea (append at end of word)
  • ce (delete word after cursor and go into insert mode)
  • cb (delete part of word before cursor and go into insert mode)
  • c i w (delete currently cursored word and go into insert mode)
Characters
  • r (change a single character)
  • d f ) (delete to the next ")" character -- and delete the character itself)
  • d F ) (delete to the previous ")" character)
  • d t ) (delete to before the next ")") [5]
  • ci { OR ci " OR ci ' (change the next bracketed/double/single/etc quoted text)
  • cit(delete within html tags)
Lines
  • c^ delete from cursor position to beginning of line and go into insert mode)
  • C (delete delete line after cursor and go into insert mode)
Blocks

Editing multiple lines with block mode is a little complex, but once you get the hang of it it is SO much easier than doing it manually. Think about commenting out lines in a bash script ;_;

  1. Use ctrl + v to enter visual block mode.
  2. Select the lines/areas you want to edit using your normal motions
  3. Press I to go into insert mode
  4. Make your edits (will only show up as one line, don't worry)
  5. Press esc, wait a second or two, and see your edits! (Note, you cannot use ctrl+c to exit out of insert mode as that immediately cancels whatever action you are in. If you are in the habit of using that to exit insert mode, and want to keep the magic alive in visual block mode, remap ctrl + c to function like esc with the following line in your vimrc vnoremap <C-c> <Esc> source)

Copy and paste

  • y - yank
  • 2 + y + w - Yank 2 words

Running shell commands from VIM

  • : opens up the command pallete
  • ! allows you to execute bash commands
  • !! re-runs the last external command (handy if you are constantly having to re-run something)
  • % refers to the open document.
  • r outputs the command to the current buffer

Ex: Run a python script you are editing. :!python % enters shell mode and prints

Ex: print the path to the current directory in the document :r !pwd

Ex: save a read-only file without having to exit and use sudo source

:w !sudo tee % (essentially, copy the current file, and write it as sudo)

Registers source

Registers are the omni-clipboard you've always wanted.

-"8 Y yank the current line to register 8 -"8 p paste the contents of register 8

EX Mode :

Basic substition:

:[range]s[ubstitute]/{pattern}/{string}/[flags] [count] (from thegeekstuff.com )

g (global pattern searching)

:g/<pattern>/<command>

Delete all blank lines: :g/^ $/d

Delete all lines that start with "compdef" :g^/compdef/d

Delete all lines that do NOT start with "compdef": :g!/^compdef/d

Resources

Firefox has vimperator but chrome has Vimium (which is far superior, imho).

Most of these tips are accessible via vimiums build in help menu shift + ?, but I am recording them here for my own edification.

Links

Copy a link's url to clipboard

y f -- Yes, this is absolutely incredible.

Mutt, Pine, and others work well. But I really really like vmail.


Reading

,u Refresh Inbox

,* Star a message

,# Delete a message

,e Archive a message

,r Reply to a message

,a Replly-All to a message

,f Forward message

Composing

,c Compose Message

,q Exit composition mode

,vs Send Message

Saving a draft

:w draft_filename.txt Saves a draft in current directory

Reloading a draft

:e draft_filename.txt Loads draft into email buffer

Sending from the command line

vmailsend < draft_filename.txt

Generating Contacts

vmail -g <number of emails> Generates list from past emails where <number of emails is a number. E.g.: vmail -g 300 Generates list from past 300 emails (pass any number you want as an arguement). Running vmail -g without passing a number scans 500 messages by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment