Skip to content

Instantly share code, notes, and snippets.

@willpower232
Last active November 14, 2022 13:54
Show Gist options
  • Save willpower232/dd46da09ced0273fffc523eaf602186f to your computer and use it in GitHub Desktop.
Save willpower232/dd46da09ced0273fffc523eaf602186f to your computer and use it in GitHub Desktop.
I am making my .bashrc portable as inspired by this https://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html
#### 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'
# I haven't used these in a while since I taught myself to use grep -R
# don't forget about -i for ignoring case and -l if you just want file names and first matches
# but you could rewrite these to use ag if you wanted but they also might not follow symlinks
#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
# just in case you're interested in history, this should include when the command was run
HISTTIMEFORMAT="%F %T "
# append history list to history file when shell exits, default?
shopt -s histappend
# append every command to the history list
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
# 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
# set PATH so it includes user's private bin if it exists
# I use a separate bin to backup standalone binaries
#if [ -d "$HOME/bin" ] ; then
# PATH="$HOME/bin:$PATH"
#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
# PROMPT_COMMAND can't see it
echo_last_status() {
if [ $? = 0 ]; then
echo "👌"
else
echo "👎"
fi
# newlines are not visible
}
# 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
#if [[ "$TERM" == "xterm-256color" && "$SHLVL" == "1" ]]; then
# echo ""
# curl --silent --max-time 2 https://wttr.in/lincoln,uk?0Q
#fi
# 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)"
argsarray=($(echo $@ | tr " " "\n"))
lastarg=${argsarray[-1]}
penultimatearg=${argsarray[-2]}
# if the penultimate arg contains @ or :// then it must be the repo url
if ! case $penultimatearg in *"@"* | *"://"*) false;; esac; then
newdir="$lastarg"
else
newdir="$(basename $lastarg .git)"
if case $penultimatearg in *":"*) false;; esac; then
newdir=${newdir#*:}
fi
fi
# this original way which doesn't support args when clone'ing
#if [ -z "$3" ]; then
# newdir="$(basename $2 .git)"
#else
# newdir="$3"
#fi
shift 1
command git clone "$@" || return 1
cd $newdir
#case $@ in
# *"workgithubname"* | *"WorkGithubName"* | *"othercompanyname"*)
# command git config user.email me@work.com && echo "set user for work boss"
# ;;
# *"personalgithubname"* | *"gist.github.com"*)
# command git config user.email me@home.com && echo "set user for personal boss"
# ;;
# *)
# echo "whoareyou"
# ;;
#esac
command git config --unset core.filemode && echo "removed core.filemode setting boss"
cd $curdir
elif [[ "$1" == "branch" && "$@" != *"--help"* && "$@" != *" -r"* && -s "$2" && -z "$3" ]]; then
if [[ "$2" == "--random" ]]; then
command git fetch --all;
while true; do name=$RANDOM; if [ -z "$(command git show-ref refs/heads/$name)" ]; then command git checkout -b $name; break; fi; done;
else
shift 1
echo "switching to checkout..."
command git checkout -b "$@" || command git checkout "$@"
fi
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 "$@"
elif [[ "$1" == "login-to-github" ]]; then
echo 'ghp_abcd' | docker login ghcr.io -u youruser --password-stdin
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. dcb --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
export DOCKER_BUILDKIT=1
alias dc="docker-compose"
alias dcb="docker-compose build --parallel"
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"
alias dceanrw="dce app npm run watch"
alias dceapat="dce app php artisan tinker"
alias dceapa="dce app php artisan"
alias dcetct="dce testapp composer test"
alias dcetca="dce testapp composer analyse"
alias dcetact="dce test-app composer test"
# 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
#}
alias assume="source assume"
alias tf="terraform"
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
source <(kubectl completion bash)
# have to manually install other bash completions
# cd $(pkg-config --variable=completionsdir bash-completion)
# curl https://raw.githubusercontent.com/ahmetb/kubectx/master/completion/kubectx.bash | sudo tee kubectx
# curl https://raw.githubusercontent.com/ahmetb/kubectx/master/completion/kubens.bash | sudo tee kubens
alias k=kubectl
complete -F __start_kubectl k
#complete -F _complete_alias k
function qk {
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 "$@"
elif [[ "$1" == "redeploy" ]]; then
shift 1
k patch deployment "$1" -p "{\"spec\": {\"template\": {\"metadata\": { \"labels\": { \"redeploy\": \"$(date +%s)\"}}}}}"
else
command kubectl "$@"
fi
}
alias kbuild='kustomize build --enable-alpha-plugins .'
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# likely to not have a UI so need to sign commits in the terminal
#export GPG_TTY=$(tty)
@willpower232
Copy link
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