Skip to content

Instantly share code, notes, and snippets.

@willpower232 willpower232/bash.bashrc
Last active Dec 23, 2019

Embed
What would you like to do?
Inspired by this https://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html I am making my .bashrc portable.
#### Set prompt basics ####
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
#### Set initial prompt ####
ps1_shelllevel='${debian_chroot:+($debian_chroot)}($SHLVL) '
if [ "$color_prompt" = yes ]; then
ps1_user='\u@\h {\A} '
# have to remember to wrap the 0-width characters on the line of the prompt in \[ and \]
if [ "$EUID" -ne 0 ]; then
ps1_folder='\e[32m[\w]'
ps1_prompt='\n\[\e[0m\]$ '
else
ps1_folder='\e[31m[\w]'
ps1_prompt='\n\[\e[0m\]# '
fi
else
ps1_user='u@h {a} '
ps1_folder='[w]'
if [ "$EUID" -ne 0 ]; then
ps1_prompt='\n$ '
else
ps1_prompt='\n# '
fi
fi
PS1=$ps1_shelllevel$ps1_user$ps1_folder$ps1_prompt
# don't unset these so they can be used later
#unset color_prompt force_color_prompt
# not sure what this bit does
#case "$TERM" in
# xterm*|rxvt*)
# PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1";;
# *)
# ;;
#esac
#### Colour support ####
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
# may be --color=auto in some versions...
alias ip='ip -c'
fi
#### Aliases for all ####
# alias commands that prefix commands
# so they can prefix your aliases
# https://askubuntu.com/a/22043
alias sudo='sudo '
alias watch='watch '
alias vim='vim -i NONE'
alias nano='nano --nowrap --smooth -T 4'
alias plz='sudo '
alias dammit='sudo !!'
# stop yourself from breaking the server by accident
alias mv='mv -i'
alias cp='cp -i'
alias ln='ln -i'
alias rm='rm -I --preserve-root'
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'
alias chgrp='chgrp --preserve-root'
function findinfiles() { find ./ -type f -print0 | xargs -0 grep -i "$1"; }
function findinphpfiles() { find ./ -type f -name "*.php" -print0 | xargs -0 grep -i "$1"; }
complete -W "$(echo $(grep '^ssh ' ~/.bash_history | sort -u | sed 's/^ssh //'))" ssh
if [ "$(uname)" == "Darwin" ]; then
# got pbcopy
:
elif grep -q Microsoft /proc/version; then
# route error to /dev/null because thanks microsoft
alias pbcopy='clip.exe > /dev/null 2>&1'
# todo: pbpaste in wsl
else
# install xclip through apt first
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
fi
# inspired by @jamesdoc https://twitter.com/jamesdoc/status/902829327187931137
# ref https://github.com/dannyfritz/commit-message-emoji
alias gc-InitialCommit="printf \"πŸŽ‰\" | pbcopy && pbpaste && echo \"\""
alias gc-VersionTag="printf \"πŸ”–\" | pbcopy && pbpaste && echo \"\""
alias gc-NewFeature="printf \"✨\" | pbcopy && pbpaste && echo \"\""
alias gc-Bugfix="printf \"πŸ›\" | pbcopy && pbpaste && echo \"\""
alias gc-SecurityFix="printf \"πŸ”’\" | pbcopy && pbpaste && echo \"\""
alias gc-Metadata="printf \"πŸ“‡\" | pbcopy && pbpaste && echo \"\""
alias gc-Refactoring="printf \"♻️\" | pbcopy && pbpaste && echo \"\""
alias gc-Documentation="printf \"πŸ“š\" | pbcopy && pbpaste && echo \"\""
alias gc-Internationalization="printf \"🌐\" | pbcopy && pbpaste && echo \"\""
alias gc-Accessibility="printf \"β™Ώ\" | pbcopy && pbpaste && echo \"\""
alias gc-Performance="printf \"🐎\" | pbcopy && pbpaste && echo \"\""
alias gc-Cosmetic="printf \"🎨\" | pbcopy && pbpaste && echo \"\""
alias gc-Tooling="printf \"πŸ”§\" | pbcopy && pbpaste && echo \"\""
alias gc-Tests="printf \"🚨\" | pbcopy && pbpaste && echo \"\""
alias gc-Deprecation="printf \"πŸ’©\" | pbcopy && pbpaste && echo \"\""
alias gc-Removal="printf \"πŸ—‘οΈ\" | pbcopy && pbpaste && echo \"\""
alias gc-WorkInProgress="printf \"🚧\" | pbcopy && pbpaste && echo \"\""
alias gc-Deforestation="printf \"🌲\" | pbcopy && pbpaste && echo \"\""
if [ -x /usr/bin/termsaver ]; then
alias tsm="termsaver matrix"
fi
if [ -x /usr/bin/cmatrix ]; then
alias etm="cmatrix -a -b -s -C cyan"
fi
#### Final settings for all ####
# this is needed to enter the passphrase when attempting to gpg-sign git commits
#export GPG_TTY=$(tty)
# if you're on WSL you may want to see information from MOTD such as pending updates
#sudo run-parts /etc/update-motd.d/
# if you're on WSL and you want to mount network shares
#sudo mount -a
# if you want to hide commands from your bash history with space and don't include duplicates
# you need this but it may already be set elsewhere
#HISTCONTROL=ignoreboth
# Debian doesn't have bash-completion by default so you need to apt install bash-completion and add the below
# also don't forget about https://github.com/cykerway/complete-alias
#if [ -f /etc/bash_completion ]; then
# . /etc/bash_completion
#fi
# don't need to do this
# /etc/bash.bashrc
# simple function to output yay or nay based on last status code
# have to include it directly in PS1 so it reads the right status code
echo_last_status() {
if [ $? = 0 ]; then
echo "πŸ‘Œ"
else
echo "πŸ‘Ž"
fi
# can't output a newline
}
# git clone git@github.com:jimeh/git-aware-prompt ~/.bash/git-aware-prompt
git_branch=
git_dirty=
if [ -d ~/.bash/git-aware-prompt ]; then
source ~/.bash/git-aware-prompt/prompt.sh
fi
if [ "$EUID" -ne 0 ]; then
if [ "$color_prompt" = yes ]; then
ps1_folder='\e[32m[\w] \e[1;34m$git_branch\e[31m$git_dirty'
else
ps1_folder='[w] $git_branch$git_dirty'
fi
fi
PS1='$(echo_last_status)\n'$ps1_shelllevel$ps1_user$ps1_folder$ps1_prompt
# lol
alias nano='vim'
# alias bashphp='docker exec -it -w "$PWD" phpfpm bash'
# alias bashnode='bash ~/containers/nodejs/interactive.sh'
# alias bashruby='bash ~/containers/ruby/interactive.sh'
# alias bashpython='bash ~/containers/python/interactive.sh'
function git {
if [[ "$1" == "clone" && "$@" != *"--help"* ]]; then
# I prefer to not include file permissions when cloning
# unfortunately this is more involved than just the below
# command git clone --config core.filemode=false "$@"
curdir="$(pwd)"
if [ -z "$3" ]; then
newdir="$(basename $2 .git)"
else
newdir="$3"
fi
shift 1
command git clone "$@"
cd $newdir
command git config --unset core.filemode
cd $curdir
echo "removed core.filemode setting boss"
elif [[ "$1" == "branch" && "$@" != *"--help"* ]]; then
shift 1
command git checkout -b "$@"
else
command git "$@"
fi
}
alias g=git
complete -F _complete_alias g
alias n=nautilus
function docker {
if [[ "$1" == "stop-and-rm" ]]; then
shift 1
command docker stop "$@"
command docker rm "$@"
else
command docker "$@"
fi
}
alias d=docker
complete -F _complete_alias d
# aliases let you put things after the command if you want
# i.e. get familiar with the flags but type way fewer characters
# e.g. dc build --no-cache -> rebuild the images without cache
# e.g. dcu -d -> dc up and detach/background it
# e.g. dce app bash -> dc exec bash in the service
# e.g. dcd -v -> dc down and clear volumes too
alias dc=docker-compose
alias dcd="docker-compose down"
alias dcu="docker-compose up"
#alias dce="docker-compose exec --user $(id -u):$(id -g) --env HOME=/tmp"
alias dce="docker-compose exec"
# persisting the changes you've made to your container
# is a terrible idea and yet here it is
function dcc {
if [ "$#" -ne "1" ]; then
echo "Precisely one argument required"
return 1
fi
CONTAINER=$(dc images | grep "_$1 " | awk '{print $1;}')
TAGNAME=$(dc images | grep "_app " | awk '{print $2;}')
TAGVERSION=$(dc images | grep "_app " | awk '{print $3;}')
if [ "$CONTAINER" = "" ] || [ "$TAGNAME" = "" ] || [ "$TAGVERSION" = "" ]; then
echo "Unable to figure out what to do"
echo "you will need to dcu -d again if you have already run this command"
return 1
fi
COMMAND="docker commit $CONTAINER $TAGNAME:$TAGVERSION"
echo $COMMAND
eval $COMMAND
}
#function k8sprod {
# bash --rcfile ~/.bashrc.k8s
#}
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
cd ~/gitrepos/k8s-configs
# https://github.com/ahmetb/kubectx
k8s_context=
k8s_namespace=
find_k8s() {
k8s_context="$(kubectx -c)"
k8s_namespace="$(kubens -c)"
}
PROMPT_COMMAND="find_k8s; $PROMPT_COMMAND"
if [ "$EUID" -ne 0 ]; then
if [ "$color_prompt" = yes ]; then
ps1_folder='\e[32m[\w] \e[1;34m$k8s_context/$k8s_namespace'
else
ps1_folder='[w] $k8s_context/$k8s_namespace'
fi
fi
PS1='$(echo_last_status)\n'$ps1_shelllevel$ps1_user$ps1_folder$ps1_prompt
export AWS_PROFILE=k8s
export AWS_SDK_LOAD_CONFIG=1
export KOPS_STATE_STORE=your-kops-state-store
function kubectl {
if [[ "$1" == "token" ]]; then
command aws-iam-authenticator token -i your.k8s.cluster -r arn:aws:iam::012345678910:role/OrganizationAccountAccessRole | jq -r .status.token | pbcopy
elif [[ "$1" == "dashboard" ]]; then
echo "Copying token to clipboard..."
qk token
echo "Done"
echo "When running, visit http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/"
k proxy
elif [[ "$1" == "watch" ]]; then
shift 1
if [[ "$1" == "get" ]]; then
shift 1
k get --watch "$@"
else
command watch -n 1 kubectl "$@"
fi
elif [[ "$1" == "podexec" ]]; then
shift 1
firstpod="$(k get pods --output=jsonpath="{.items[0].metadata.name}" | head -1)"
echo "Running in $firstpod"
k exec -it $firstpod "$@"
else
command kubectl "$@"
fi
}
alias k=kubectl
complete -F _complete_alias k
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
@willpower232

This comment has been minimized.

Copy link
Owner Author

willpower232 commented Dec 11, 2016

Ideally you would include this as part of your main .bashrc but it is possible to include it for your session so as to not disrupt other people using the same login using either of the following:

  • local copy $ . <(cat my.bashrc)
  • remote copy $ . <(curl -Ss https://onlinecopysomewhere.com)
  • remote copy with shortcut domain $ . <(curl -LSs https://handyshortcutdomain.com)

Easy install system-wide (you may want to back up the original file as it will get overwritten)
$ curl -LSs https://handyshortcutdomain.com | sudo tee /etc/bash.bashrc > /dev/null

Don't forget your ~/.profile if you SSH into your system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.