Get Productive with Shell Aliases


Over the years, I have collected a number of shell aliases from various blog posts and websites that have tremendously helped me navigate and automate confusing workflows and remember complicated terminal commands. This post, for what it’s worth, is about giving back to the developer community with me sharing aliases I find to be extremely useful in hopes that you, dear reader, also find them useable and productive.

There may be better options for housing such aliases, but for better or worse, all commands are to be put inside ~/.profile, all of which have been exercised on macOS.

Activate Profile

alias editp='edit ~/.profile'
alias actp='source ~/.profile'

Change Directory

alias cd..='cd ../'                         # Go back 1 directory level (for fast typers)
alias ..='cd ../'                           # Go back 1 directory level
alias ...='cd ../../'                       # Go back 2 directory levels
alias .3='cd ../../../'                     # Go back 3 directory levels
alias .4='cd ../../../../'                  # Go back 4 directory levels
alias .5='cd ../../../../../'               # Go back 5 directory levels
alias .6='cd ../../../../../../'            # Go back 6 directory levels

Editors

alias edit="code" # Visual Studio Code
alias this="edit ."
alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl $@"
alias this="edit ."

Variables for Colorful Output

BOLD=$(tput bold)
BLACK=$(tput setaf 0)
WHITE=$(tput setaf 7)
BLUE=$(tput setaf 4)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)

Mastering the SSH Agent

export env=$HOME/.ssh/environment

function agent_is_running() {
  if [ "$SSH_AUTH_SOCK" ]; then
    # ssh-add returns:
    #   0 = agent running, has keys
    #   1 = agent running, no keys
    #   2 = agent not running
    ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]
  else
    false
  fi
}

function agent_has_keys() {
  ssh-add -l >/dev/null 2>&1
}

function agent_load_env() {
  . "$env" >/dev/null
}

function agent_start() {
  echo "Starting SSH agent..."
  (umask 077; ssh-agent >"$env")
  . "$env" >/dev/null
}

function add_all_keys() {
  echo "Adding SSH keys..."
  ls ~/.ssh | grep ^id_rsa.*$ | sed "s:^:`echo ~`/.ssh/:" | xargs -n 1 ssh-add
}

if ! agent_is_running; then
  agent_load_env
fi

# if your keys are not stored in ~/.ssh/id_rsa.pub or ~/.ssh/id_dsa.pub, you'll need
# to paste the proper path after ssh-add
if ! agent_is_running; then
  agent_start
  add_all_keys
elif ! agent_has_keys; then
  add_all_keys
fi

echo `ssh-add -l | wc -l` SSH keys registered.

unset env

Fancy Git Prompt

if [ -f "$(brew --prefix bash-git-prompt)/share/gitprompt.sh" ]; then
    GIT_PROMPT_THEME=Default
    source "$(brew --prefix bash-git-prompt)/share/gitprompt.sh"
fi

Switch JDK Versions

function setjdk() {
  if [ $# -ne 0 ]; then
   removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
   if [ -n "${JAVA_HOME+x}" ]; then
    removeFromPath "$JAVA_HOME/bin"
   fi
   export JAVA_HOME=`/usr/libexec/java_home -v $@`
   export PATH=$JAVA_HOME/bin:$PATH
   echo -e "JAVA_HOME to $@"
  fi
}

function removeFromPath() {
  export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}

…where you can do:

setjdk 1.8

or:

setjdk 11

Spawn Databases via Docker

function mysql() {
    docker run --name mysql -p3306:3306 --env="MYSQL_ROOT_PASSWORD=password" --env="MYSQL_DATABASE=test" -d mysql
}

function sqlserver() {
    docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=p@ssw0rd' -p 1433:1433 microsoft/mssql-server-linux:2017-CU7
}

Gradle

Dependency Insight

function gdep() {
  ./gradlew dependencyInsight --configuration "$2" --dependency "$1"
}

…where you can do:

gdep compileClasspath jackson

Properties

alias gprop="edit ~/.gradle/gradle.properties"

Helpers

alias openPorts='sudo lsof -i | grep LISTEN'

alias cls="clear"
alias del='rm'
alias path='echo -e ${PATH//:/\\n}'

alias hostfile="sudo atom /etc/hosts" # Requires Atom

function mcd() {
  mkdir -p "$1" && cd "$1";
}

alias dir="ls -a -l"
alias myip="curl ifconfig.co/json"

Docker

Search & Destroy

alias dkc="docker stop $(docker ps -aq); docker rm $(docker ps -aq)";

SSH Into Container

function dockerssh() {
    export CID=$(docker ps -aqf "name=$1"); docker exec -it $CID /bin/bash
}

Git

Commit Changes

function gc() {
  echo -e "${GREEN}Commit message:\n${WHITE}\t$1\n${NORMAL}"

  echo "Adding all changes..."
  git add --all
  echo "Committing changes..."
  git commit -S -am "$1"

  echo "Fetching all submodules..."
  modules=($(git submodule | awk '{print $2}'))

  for module in "${modules[@]}"; do
    echo -e "\tSwitching to module ${WHITE}${module}${NORMAL}"
    pushd $module > /dev/null
    echo -e "\tFetching status for module ${WHITE}${module}${NORMAL}"
    modulestatus=$(git status --porcelain | wc -l)
    if [[ $modulestatus -ne 0 ]]; then
      echo -e "\t${GREEN}Updating module: ${WHITE}${module}${NORMAL}"

      echo -e "\tAdding changes for module ${WHITE}${module}${NORMAL}"
      git add --all
      echo -e "\tCommitting changes for module ${WHITE}${module}${NORMAL}"
      git commit -S -am "$1"
      git status --short
    else
      echo -e "\tNo changes found to commit for module ${WHITE}${module}${NORMAL}"
    fi
    popd > /dev/null
  done
  echo -e "${GREEN}Done!\nStatus:${NORMAL}"
  git status
}

Misc

alias delmerged="git branch --merged | grep -v "\*" | grep -v master | grep -v dev | xargs -n 1 git branch -d"
alias gco="git checkout $1"
alias gbo="git checkout -b $1"
alias gp="git push --set-upstream $1 $2"
alias gl="git pull $1 $2"
alias grs="git reset --hard && git clean -fd"
alias gba="git branch -a"
alias gbr="git branch -r"
alias gs="git status -s -b"
alias gpl="git pull origin master --no-edit --allow-unrelated-histories"
alias gps="git push origin master --recurse-submodules=on-demand "
alias gst="git status"
alias ggc="git gc --aggressive --prune=now"

Processes

function pidit() {
    ps -ef | grep "$@" | grep -v grep |  awk '{print $2}'
}

function proc() {
    ps -ef | grep "$@" | grep -v grep
}

function kp() {
    ps -ef | grep "$1" | grep -v grep | awk '{print $2}' | xargs kill
}

Search Aliases

function cm() {
    alias | grep "$@" | head -n 1
}

Contribute to Apereo Blog

Once you have cloned the Apereo Blog repository:

alias blog="cd /path/to/apereo.github.io"
alias blogthis="blog; this"

That’s it. I hope you find these useful.

Misagh Moayyed

Related Posts

CAS 6.1.0 RC4 Feature Release

...in which I present an overview of CAS 6.1.0 RC4 release.

Apereo CAS - Multifactor Provider Selection

Learn how to configure CAS to integrate with and use multiple multifactor providers at the same time. This post also reveals a few super secret and yet open-source strategies one may use to select appropriate providers for authentication attempts, whether automatically or based on a menu.

Apereo CAS - Dockerized Hazelcast Deployments

Learn how to run CAS backed by a Hazelcast cluster in Docker containers and take advantage of the Hazelcast management center to monitor and observer cluster members.

Apereo CAS - Configuration Security w/ Jasypt

Learn how to secure CAS configuration settings and properties with Jasypt.

CAS 6.1.0 RC3 Feature Release

...in which I present an overview of CAS 6.1.0 RC3 release.

Apereo CAS - Webflow Decorations

Learn how you may decorate the Apereo CAS login webflow to inject data pieces and objects into the processing engine for display purposes, peace on earth and prosperity of all mankind, etc. Mainly, etc.

Apereo CAS - SAML2 Metadata Query Protocol

Learn how you may configure Apereo CAS to fetch and validate SAML2 metadata for service providers from InCommon's MDQ server using the metadata query protocol.

Saving Time is Time Consuming

May you live in the best of times. May you live in the startup times.

Apereo CAS 6.1.x - Credential Caching & Proxy AuthN

Learn how you may configure Apereo CAS to capture and cache the credential's password and the proxy-granting ticket in proxy authentication scenarios, pass them along to applications as regular attributes/claims. We will also be reviewing a handful of attribute release strategies that specifically affect authentication attributes, conveying metadata about the authentication event itself.

Apereo CAS 6.1.x - Attribute Repositories w/ Person Directory

An overview of CAS attribute repositories and strategies on how to fetch attributes from a variety of sources in addition to the authentication source, merge and combine attributes from said sources to ultimately release them to applications with a fair bit of caching.