Skip to content

Instantly share code, notes, and snippets.

@electron0zero
Last active October 22, 2020 18:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electron0zero/b7b54dcb619a0f5c8f2fd29e0e345cc9 to your computer and use it in GitHub Desktop.
Save electron0zero/b7b54dcb619a0f5c8f2fd29e0e345cc9 to your computer and use it in GitHub Desktop.
my ~/.bash_aliases and kubectl_aliases
# === Bash functions that are used in aliases
# get bash shell on docker container.
# works only if one container is running
function get_bash_docker() {
count=$(docker ps -q | wc -l)
if [[ $count = 1 ]]; then
container_id=$(docker ps -q)
echo "Getting bash shell in ${container_id}"
docker exec -it ${container_id} bash
else
# do not show running container if no containers are running
if [[ $count = 0 ]]; then
echo "No running containers"
return 0
fi
# show running containers
echo "Can't decide, $count containers are running"
echo $(docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Image}}")
echo "SSH into container using CONTAINER ID"
echo "docker exec -it <CONTAINER ID> bash"
return 1
fi
}
# === Aliases
# put your alias here, this file in socured in .bashrc
# folder navigation
alias wd='cd ~/wd/'
alias dl='cd ~/Downloads/'
# copy to clipboard
alias copy='xclip -sel clip'
# git alias
# git branches sorted by last committed date
alias gbsc='git branch --sort=-committerdate --color=always | more -10'
alias glo='git log --oneline --color=always | more -10'
alias gits='git status'
alias gd='git diff HEAD'
# only diff staged files
alias gds='git diff --cached'
# stash helpers
alias gsl='git stash list'
alias gst='git stash'
alias gsp='git stash pop'
# sync upstream, local and origin
alias gup='git checkout master && git pull'
# alias for tools
alias gi='grep -i'
alias ls='ls -lah --color'
# Docker aliases
alias dssh="get_bash_docker"
# alias for music player controls
# needs playerctl (https://github.com/acrisci/playerctl)
alias n='playerctl next'
alias p='playerctl previous'
alias pp='playerctl play-pause'
alias pw='playerctl metadata'
# NOTE: these lines are added in vanila Ubuntu bashrc
# curl_time see time taken to pull a url via curl
# https://stackoverflow.com/a/47944496/5209755
curl_time() {
curl -so /dev/null -w "\
namelookup: %{time_namelookup}s\n\
connect: %{time_connect}s\n\
appconnect: %{time_appconnect}s\n\
pretransfer: %{time_pretransfer}s\n\
redirect: %{time_redirect}s\n\
starttransfer: %{time_starttransfer}s\n\
-------------------------\n\
total: %{time_total}s\n" "$@"
}
# bash script to get title of a url and then dump it in Markdown format link
# credit: https://unix.stackexchange.com/a/103253
function url_to_md( ) {
t=$(wget -qO- $1 | perl -l -0777 -ne 'print $1 if /<title.*?>\s*(.*?)\s*<\/title/si')
echo "[$t]($1)"
}
# https://gist.github.com/electron0zero/7685fca7c2ae4d0cd355aa670675b65f
# https://unix.stackexchange.com/a/26950/270668
export PROMPT_DIRTRIM=3
# used to control length after truncate
truncate_len=15
truncate() {
echo "$@" | \
awk -v len=$truncate_len '{ if (length($0) > len) print substr($0, 0, len) ".." ; else print; }'
}
git_branch() {
branch=$(git branch 2>/dev/null | grep '^*' | colrm 1 2)
truncate "$branch"
}
has_mod_files(){
if [[ $(git ls-files -dmo --exclude-standard 2> /dev/null) ]]; then
echo "*"
else
echo ""
fi
}
export PS1="\[\e[43m\]\[\e[30m\]\\w\[\e[42m\]\[\e[30m\][\$(git_branch)\$(has_mod_files)]\[\e[01;00m\] $ "
# nodejs stuff
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
# kubectl auto completion
source "$HOME/.kube/completion.bash.inc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment