bash#

Aliases#

Setup

Aliases can either be added directly to ~/.bashrc or to ~/.bash_aliases.

If added to ~/.bash_aliases, it has to be ensured that this file is sourced from ~/.bashrc:

# ~/.bashrc
if [ -f ~/.bash_aliases ]; then
  . ~/.bash_aliases
fi

History Search with Arrows

These aliases allow to search in the history based on the already typed characters.

bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
bind '"\e[C": forward-char'
bind '"\e[D": backward-char'

Convenient directory handling

alias ..='cd ..'
alias ..2='cd ../..'
alias ..3='cd ../../..'
alias ..4='cd ../../../..'
alias ..5='cd ../../../../..'
alias ..6='cd ../../../../../..'

Run and connect to a Docker Container

function docker_ubuntu() {
        NAME=""
        if [[ $# -ne 0 ]]; then
                NAME="--name $1"
        fi

        docker run $NAME -it --rm --network host \
                -v $PWD:$PWD \
                -v ~/.ssh:/home/docker/.ssh \
                -v ~/.gitconfig:/home/docker/.gitconfig:ro \
                -w $PWD \
                ubuntu:22.04 /bin/bash
}
function docker_exec() {
        if [[ $# -eq 0 ]]; then
                echo "Please provide name of the docker container!"
        else
                docker exec -it $1 /bin/bash
        fi
}

Enable VNC on a Remote Host via SSH

function tower_vnc() {
        # <user> and <ip> has to be set accordingly!
        ssh -fL 6001:localhost:5901 <user>@<ip> sleep 10
        xtigervncviewer -SecurityTypes VncAuth -passwd /home/<user>/.vnc/passwd localhost:6001
}

Custom Bash Prompt#

# Default Colors
RGB_COLOR_WHITE='254'
RGB_COLOR_GREEN='28'
RGB_COLOR_YELLOW='136'
RGB_COLOR_RED='124'

# Colors => Exit Code
RGB_COLOR_EXIT_CODE_OK_FG=$RGB_COLOR_GREEN
RGB_COLOR_EXIT_CODE_ERROR_FG=$RGB_COLOR_RED

# Colors => User
RGB_COLOR_USER_BG='88'
RGB_COLOR_USER_FG=$RGB_COLOR_WHITE

# Colors => Directory
RGB_COLOR_DIR_BG='236'
RGB_COLOR_DIR_FG=$RGB_COLOR_WHITE

# Colors => Git
RGB_COLOR_GIT_OK_BG=$RGB_COLOR_GREEN
RGB_COLOR_GIT_DIRTY_BG=$RGB_COLOR_RED
RGB_COLOR_GIT_DELETED_BG=$RGB_COLOR_RED
RGB_COLOR_GIT_UNTRACKED_BG=$RGB_COLOR_YELLOW
RGB_COLOR_GIT_NEWFILE_BG=$RGB_COLOR_YELLOW
RGB_COLOR_GIT_RENAMED_BG=$RGB_COLOR_RED
RGB_COLOR_GIT_AHEAD_BG='30'
RGB_COLOR_GIT_FG=$RGB_COLOR_WHITE

# Colors => User Input
RGB_COLOR_INPUT_FG=$RGB_COLOR_WHITE

# Color Reset
COLOR_RESET="\e[0m"

# Special Characters
SEPARATOR=$'\uE0B0'

# Color Handling
# $1: Foreground Color
# $2: Background Color (optional)
function set_color {
  if [ "$#" -eq 2 ]; then
    echo -e "\e[0;38;5;$1;48;5;$2m"
  elif [ "$#" -eq 1 ]; then
    echo -e "\e[0;38;5;$1;49m"
  fi
}

# Color Handling
# $1: Foreground Color
# $2: Background Color (optional)
function set_color_bold {
  if [ "$#" -eq 2 ]; then
    echo -e "\e[1;38;5;$1;48;5;$2m"
  elif [ "$#" -eq 1 ]; then
    echo -e "\e[1;38;5;$1;49m"
  fi
}

# Git Handling => Current Branch or Commit
function git_branch {
  local GIT_STATUS="$(LANG=en_US.UTF-8 git status 2> /dev/null)"
  local ON_BRANCH="On branch ([^${IFS}]*)"
  local ON_COMMIT="HEAD detached at ([^${IFS}]*)"

  if [[ $GIT_STATUS =~ $ON_BRANCH ]]; then
    echo " ${BASH_REMATCH[1]} "
  elif [[ $GIT_STATUS =~ $ON_COMMIT ]]; then
    echo " ${BASH_REMATCH[1]} "
  fi
}

# Git Handling => Status
function git_status {
  LANG=en_US.UTF-8 git status 2> /dev/null | (
    unset dirty deleted untracked newfile ahead renamed
    while read line ; do
        case "$line" in
          *modified:*)                      dirty='!' ; ;;
          *deleted:*)                       deleted='x' ; ;;
          *'Untracked files:')              untracked='?' ; ;;
          *'new file:'*)                    newfile='+' ; ;;
          *'Your branch is ahead of '*)     ahead='*' ; ;;
          *renamed:*)                       renamed='>' ; ;;
        esac
    done
    bits="$dirty$deleted$untracked$newfile$ahead$renamed"
    [ -n "$bits" ] && echo " $bits" || echo "c"
  )
}

# Git Handling => Bg Color depending on the Status
function git_bg_color {
  local GIT_STATUS=$(git_status)

  if [[ -n $GIT_STATUS ]]; then
    if [[ $GIT_STATUS =~ "!" ]]; then
      echo $RGB_COLOR_GIT_DIRTY_BG
    elif [[ $GIT_STATUS =~ "x" ]]; then
      echo $RGB_COLOR_GIT_DELETED_BG
    elif [[ $GIT_STATUS =~ "?" ]]; then
      echo $RGB_COLOR_GIT_UNTRACKED_BG
    elif [[ $GIT_STATUS =~ "+" ]]; then
      echo $RGB_COLOR_GIT_NEWFILE_BG
    elif [[ $GIT_STATUS =~ ">" ]]; then
      echo $RGB_COLOR_GIT_RENAMED_BG
    elif [[ $GIT_STATUS =~ "*" ]]; then
      echo $RGB_COLOR_GIT_AHEAD_BG
    else
      echo $RGB_COLOR_GIT_OK_BG
    fi
  fi
}

# Build Info about the last Exit Code
# $1: Last Exit Code
function set_exit_code {
  if [ $1 -eq 0 ]; then
    PS1_EXIT_CODE="\[\$(set_color $RGB_COLOR_EXIT_CODE_OK_FG $RGB_COLOR_USER_BG)\]"
    PS1_EXIT_CODE+=" ✔"
  else
    PS1_EXIT_CODE="\[\$(set_color $RGB_COLOR_EXIT_CODE_ERROR_FG $RGB_COLOR_USER_BG)\]"
    PS1_EXIT_CODE+=" ✘"
  fi
}

# Build the Common Info
function set_common_info {
  PS1_COMMON_INFO="\[\$(set_color $RGB_COLOR_USER_FG $RGB_COLOR_USER_BG)\]"
  PS1_COMMON_INFO+=" \u "
  PS1_COMMON_INFO+="\[\$(set_color $RGB_COLOR_USER_BG $RGB_COLOR_DIR_BG)\]"
  PS1_COMMON_INFO+="$SEPARATOR"
  PS1_COMMON_INFO+="\[\$(set_color $RGB_COLOR_DIR_FG $RGB_COLOR_DIR_BG)\]"
  PS1_COMMON_INFO+=" \w "
}

# Build the Git Info
function set_git_info {
  local GIT_BRANCH="$(git_branch)"
  if [ -n "$GIT_BRANCH" ]; then
    local GIT_BG_COLOR="$(git_bg_color)"

    PS1_GIT_INFO="\[\$(set_color $RGB_COLOR_DIR_BG $GIT_BG_COLOR)\]"
    PS1_GIT_INFO+="$SEPARATOR"
    PS1_GIT_INFO+="\[\$(set_color $RGB_COLOR_GIT_FG $GIT_BG_COLOR)\]"
    PS1_GIT_INFO+="$GIT_BRANCH"
    PS1_GIT_INFO+="\[\$(set_color $GIT_BG_COLOR)\]"
    PS1_GIT_INFO+="$SEPARATOR"
  else
    PS1_GIT_INFO="\[\$(set_color $RGB_COLOR_DIR_BG)\]"
    PS1_GIT_INFO+="$SEPARATOR"
  fi
}

# Build the User Input
function set_input {
  PS1_INPUT="\[\$(set_color_bold $RGB_COLOR_INPUT_FG)\]"
  PS1_INPUT+=" \$ "
  PS1_INPUT+="\[$COLOR_RESET\]"
}

# Overwrite the Prompt Command
PROMPT_COMMAND=__prompt_command
__prompt_command() {
  local EXIT_CODE="$?"

  # Build string regarding the last exit code
  set_exit_code $EXIT_CODE

  # Build string regarding the common info (user, directory, ...)
  set_common_info

  # Build string regarding the git status
  set_git_info

  # Build string regarding the user input
  set_input

  # Build Prompt
  if (tput cuu1 && tput sc && tput rc && tput el) >/dev/null 2>&1
  then
    PS1="
${PS1_EXIT_CODE}${PS1_COMMON_INFO}${PS1_GIT_INFO}${PS1_INPUT}"
    PS2="\[$(tput el)\]> "
    trap 'tput el' DEBUG
  fi
}