2/9/2017 - 11:49 PM

saving time on the command-line

saving time on the command-line

Terminal Productivity Tricks

Line Editing Motions

  • [C-a] beginning of the line
  • [C-e] end of the line
  • [C-b] back one character
  • [C-f] forward one character
  • [A-b] back one word (Configure alt to +ESC in iterm profile)
  • [A-f] forward one word
  • [C-u] delete to beginning of line
  • [C-w] delete word
  • [C-y] paste
  • [A-d] delete word in front

Shell Tricks

  • $_ or [ESC].

    • Grabs the last arg from the last command.
    • example:
    vim ~/.bashrc
    source $_
    ls ~/workspace/lumos_rails/config/
    cat $_/[TAB]file.txt
  • ^replace^word

    • replaces the very first occurrence of the first characters with the second
    • example:
    memroy show production lumosity
    -bash: memroy: command not found
    memory show production lumosity
  • command subtitution: $(cmd), `cmd`

    • Executes the command and uses the output of it as args
    • Can be used like xargs
    • example:
    echo $VAR
    seq 1 10
    for n in `seq 1 10`; do echo "loop $n"; done;
  • !!, ![NUMBER]

    • execute the last command / command at history number
    • example:
    vim /etc/hosts
    sudo !!
    which memory
    vim `!!`
  • Curly Brace Expansion

    • Specify multiple similarly named args
    • example:
    cp /etc/hosts{,.bak}
    sudo !! # <-- see what i did thar
    touch ~/workspace/long_filename_with_bad_file_extensiont.xt
    mv ~/workspace/long_filename_with_bad_file_extension{t.xt,.txt}
  • reverse-i-search [C-r]

    • search for a matching command previously entered
  • cd -

    • change to directory you were previously in
    • example:
    [~] cd ~/workspace/
    [~/workspace] cd -
  • [C-x][C-e] (in-terminal editor only)

    • opens your currently typed command in an editor
    • saving and quitting will execute the command
    • example:
    for f in `ls ~`; do [C-x][C-e]
    echo "file: $f";
  • [C-z], fg

    • suspends the current process / foregrounds the process
    • example:
    vim some_script.rb
    ruby some_script.rb


  • alias lock='/System/Library/CoreServices/Menu\ Extras/ -suspend'

    • lock your computer from the terminal
    • does not kill network connections
  • alias git=hub

    • see command section on hub
  • alias ls='ls -F'

    • Adds symbols that makes it easier to distinguish types of files
  • Better cd. Find yourself running cd; ls all the time?

    • Add to ~/.bashrc:
    c() { cd "$@" && ls; }
  • alias hgrep="history | grep -i"

    • Quickly search your terminal history for a command
    • Use in conjunction with ![NUMBER]
    • Add these to your ~/.bashrc:
    export HISTSIZE=500000
    export HISTFILESIZE=100000
    export HISTIGNORE="&:exit:bg:fg:clear"
    export HISTTIMEFORMAT="%m/%d/%y %T "
    export HISTCONTROL=ignoreboth:erasedups # ignore commands starting with a space, and ignore duplicates
  • Get current branch

    • Add to ~/.bashrc:
    alias tgb='git symbolic-ref --short HEAD'
    • example:
    gpo `tgb`
  • My git aliases

    • Add to ~/.bashrc:
    alias pull="git pull origin"
    alias gb="git branch"
    alias gl="git log"
    alias glp="git log -p"
    alias gs="git status"
    alias gc="git commit -m"
    alias ga="git add"
    alias gck="git checkout"
    alias gd="git diff"
    alias gdc="git diff --cached"

Useful Commands

  • pbcopy, pbpaste (mac exclusive)

    • Copies STDIN into your clipboard / pastes your clipboard
    • example:
    grep alias ~/.bashrc | pbcopy
    pbpaste > /tmp/my_aliases
    pbpaste | wc -l
  • watch

    • continuously executes a command
    • example:
    gem install iStats # <-- capital S
    istats all --no-graphs
    watch -n1 !!
  • hub

    • GitHub's wrapper around git
    • brew install hub
    • be sure to alias hub over git
    • example:
    git clone lumoslabs/knife_memory
    git checkout -b test_hub_command
    echo "test" > TEST
    git add TEST
    git commit -m 'Testing hub'
    git push origin `tgb`
    git browse
    git pull-request
  • curl

    • obtain your public IP
    • useful on a remote server or checking your proxy
  • htop

    • the modern top
    • brew install htop
  • reset

    • restores your terminal
    • example:
    [32605:mleung-mbp][~] cat /sbin/* # <-- for whatever reason I can only reproduce in terminal
                                                                          ??Dg???-ö6?? ??.)?掲å?Ä?ÅV?3?fÄ32605:mleung-mbpÅÄüÅ 1;2c
    Ä32605:mleung-mbpÅÄüÅ reset