Essential Command line Tools

  • bourne shell scripting: for hacky-small tools as well as command line use learn how to use && and || -- much more concise than if/then/else/fi
  • find + xargs
  • sed, perl, grep (-A,-B,-C,-v), awk for extracting columns
  • use && to concat commands instead of ; so right hand command will only run if left hand command succeeds

cool, lesser-known tools

  • glark
  • GNU parallel

Use Vim keystrokes on the command line

"vi editing mode" (the default is "emacs mode")

set -o vi

add this to ~/.bashrc

# Enable Vi key bindings
set -o vi

Use mosh instead of ssh


brew install mobile-shell

Use tmux

Why does using a terminal multiplexer help?

upon laptop crash/reboot, you lose no state from the multiplexed terminal sessions on your devserver
you can search scrolled screen content quickly and mechanically
you can easily disconnect/reconnect a "session" -- no more HUPs
e.g., you're in the middle of a 20-minute run that you did not protect via nohup, when you need to go to a meeting or catch a bus. With neither mosh nor screen/tmux, you'd lose the ssh connection.

Tell tools to save your history

echo HISTSIZE=130000 HISTFILESIZE=-1 >> ~/.bashrc
echo set -g history-limit 30000 >> ~/.tmux.conf

Avoid unnecessary prompts (avoid slow-down and risk of wrong answer)

Use --backup options to cp, ln, mv:

alias cp='cp --backup=numbered'
alias ln='ln --backup=numbered'
alias mv='mv -f --backup=numbered'

This option is only supported by GNU coreutils, not by the BSD versions that come with OS X. It’s possible to install GNU coreutils, e.g. with brew install coreutils. To override the builtins, follow the instructions printed by brew info coreutils. Use the "env " prefix to avoid the alias and instead invoke the bare program.

Use incremental search (^R, aka Control-R) #

  • in your editor
  • when searching shell history
  • when searching through contents of scrolled screen log
  • in gdb
  • in any other readline-based tool (they all honor ~/.inputrc)

If you use vi/vim, run this and restart any existing bash shell:

echo set editing-mode vi >> ~/.inputrc

If you use zsh in vi mode, add this to ensure ^R still does what we expect:

echo 'bindkey "^R" history-incremental-search-backward' >> ~/.zshrc

Some custom key bindings

~/.inputrc is the config file for readline, which is used in many interactive programs such as bash and gdb. The following key bindings will make your life easier. Remember to restart bash after modifying ~/.inputrc.

Cycle through commands to a partial match

Add the following two lines to your ~/.inputrc (note all the quotation marks, don't echo it!).

echo '"\e[A": history-search-backward' >> ~/.inputrc
echo '"\e[B": history-search-forward' >> ~/.inputrc

Type in a few chars of a command in history and use arrow (up/down) keys to cycle through all commands that start with the typed in chars.

Delete word for bash command line (^W, aka Control-W)

echo '"\C-w": backward-delete-word' >> ~/.inputrc

Setting vim as your default

Many command-line utilities check the $EDITOR and $VISUAL environment variables to determine how to open files for editing or display.

export EDITOR=vim
export VISUAL=vim

to your ~/.profile or ~/.bashrc.

To use vim for git, add


to your ~/.gitconfig.


syntax on
set ts=4
set shiftwidth=4
set expandtab

Add syntax highligh

syntax on

Use soft tab(spaces instead of tabs)

set expandtab


ls: color by file extension

To show colors by file extension

$ ls -G

To make it default, add this to ~/.bash_profile

alias ls="ls -G"

Style your bash

Note: in Mac OS X, open Terminal->Preferences, choose "Shells open with" as "Command(complete path)", and set it bin/bash to use ~/.bash_profile settings.

function prompt {
    local      BLACK="\[\033[0;30m\]"
    local        RED="\[\033[0;31m\]"
    local  LIGHT_RED="\[\033[1;31m\]"
    local      GREEN="\[\033[0;32m\]"
    local LIGHT_GREEN="\[\033[1;32m\]"
    local      YELLOW="\[\033[0;33m\]"
    local        BLUE="\[\033[0;34m\]"
    local    MAGENTA="\[\033[0;35m\]"
    local        CYAN="\[\033[0;36m\]"
    local      WHITE="\[\033[1;37m\]"
    local  LIGHT_GRAY="\[\033[0;37m\]"

    PS1="\n${RED}\[email protected]\h:${BLUE}\$(parse_git_branch) ${GREEN}\w ${BLACK}\n\$ "

Add Git Branch or Mercurial Bookmark to Prompt

To have your command prompt show your current git branch or mercurial bookmark, add this to your ~/.bashrc file:

source $ADMIN_SCRIPTS/scm-prompt
export PS1='\[email protected]\h:\w $(_dotfiles_scm_info)\$ '


    IdentityFile /path/to/private/key.pem

For example

Host foo
    User stack
    IdentityFile /path/to/private/key.pem


$ ssh foo

is essentially the same as

$ ssh -i /path/to/private/key.pem [email protected]

fundamental eight

Learn grep, sed, cut, sort, uniq, head, tail, and xargs. These eight tools make up the commands you'll be running most often on the command line when doing text processing. You can use perl -ne/-pe as a dropin replacement for grep, sed, cut, and awk, which gives you richer regexes and logic if you need them.

perl -ne Equivalent to grep, but you can also run logic too and do more complicated extraction perl -pe Just like sed but with badass perl logic. Executes the expression before printing each line.


grep | wc -> grep -c
sort | uniq | wc -> sort -u | wc 

Show hg diff

Show changes in current commit:

hg diff -c 

Add diff alias

function diff_fn() {
  if [ -z $1 ]
    hg diff -r .^
    x=$(hg book | grep -e "$1 ")
    hg diff -c ${x: -12}

alias diff=diff_fn

Links and References

General command-line tips: Perl one-liners: Copy from terminal output (no mouse): Regex learning tools: the art of command line tips like this?

learn vim This one is free: And of course, there is the command-line vimtutor