Heyo this is my bash profile. I make a toooon of personal scripts for ease of use.
If you haven’t already noticed, this is an org file, not a bash file. I use a couple scripts to complie this to bash and then execute it.
Env vars this depends on - need to be set in local bash profile. This is a poor man’s form of dependency injection enforcement in bash. Maybe there’s a better way to futz with env vars that should be defined elsewhere, but I don’t know it.
function checkEnvAndDefault() {
if [ -z `printenv $1` ];
then
echo "you should probably export $1 in the local bash profile"
export `echo $1`="YOUFORGOTTOSETTHIS"
fi
}
And these are the vars I want defined by the local system.
checkEnvAndDefault "PATH_TO_DOTFILES_REPO"
Go powerline
export GOPATH="$HOME/go"
export GOBIN="$HOME/go/bin"
export EXTRA_POWERLINE_DIR_ALIASES="$EXTRA_POWERLINE_DIR_ALIASES,~/code/dotfiles=dfiles"
checkEnvAndDefault "EXTRA_POWERLINE_OPTIONS"
function _update_ps1() {
PS1="$($GOPATH/bin/powerline-go -error $? -jobs $(jobs -p | wc -l) -shell bash -cwd-max-dir-size 2 -colorize-hostname -hostname-only-if-ssh -truncate-segment-width 5 -path-aliases=$EXTRA_POWERLINE_DIR_ALIASES $EXTRA_POWERLINE_OPTIONS)"
# Uncomment the following line to automatically clear errors after showing
# them once. This not only clears the error for powerline-go, but also for
# everything else you run in that shell. Don't enable this if you're not
# sure this is what you want.
set "?"
}
if [ -f "$GOPATH/bin/powerline-go" ]; then
PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
else
echo "powerline-go not installed, go get it https://github.com/justjanne/powerline-go#installation"
fi
Utility function to spit out a thing to STDOUT and then evaluate it. Useful because I want to see what my aliases are doing in many cases.
function echoAndEval {
echo $1
eval $1
}
Here’s something that never panned out:
alias a='alias'
Quick clear
alias c='clear'
Shortcuts to open local dotfiles. Generally, these files source something in this repo for ease of bootstrapping config on new machines.
alias vlv='vim ~/.vimrc'
alias vle='vim ~/.emacs.d/init.el'
alias vlbp='vim ~/.bash_profile'
alias slbp='echo "sourcing LOCAL bash profile"; . ~/.bash_profile'
These require the aliases used to be defined locally so this can figure out the path. Some of them are of the form “vx” which in my brain is “vim x”. I have cleverly redirected org files to emacs, even though I still type “ve” for “vim emacsconfig”.
alias ve='echoAndEval "emacs $PATH_TO_DOTFILES_REPO/dotEmacs.org"'
Quick sourcing and vimming of this bash profile
alias sbp='echo "sourcing bash profile"; . $PATH_TO_DOTFILES_REPO/bash_profile.bash'
alias vbp='emacs $PATH_TO_DOTFILES_REPO/bashProfile.org'
Dropbox paths are probably the same everywhere? Fingers crossed!
alias inbox='emacs ~/Dropbox/org/inbox.org'
alias ptodos='emacs ~/Dropbox/org/projects/homeProjects.org'
alias funthings='emacs ~/Dropbox/org/projects/funThings.org'
alias homeprojects='emacs ~/Dropbox/org/projects/homeProjects.org'
alias pproj='emacs ~/Dropbox/org/projects/personalProjects.org'
alias workstuff='emacs ~/Dropbox/org/projects/workInbox.org'
alias winbox='workstuff'
alias recurring='emacs ~/Dropbox/org/projects/recurring.org'
Quick navigation to my org directory.
alias cdo='cd ~/Dropbox/org/projects/'
alias cdoroot='cd ~/Dropbox/org/'
alias cdor='cdoroot; cd roam'
alias cdord='cdor; cd daily'
Completely stolen from Dustin Bennett
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../../'
alias ....='cd ../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'
alias cddf='echoAndEval "cd $PATH_TO_DOTFILES_REPO"'
function debug {
echo $x
}
I always mess up “grep -r stuff .” and “grep -r . stuff” vendor is an exclusion of php libraries - I should probably add other libarary directories but I’m mostly writing php day to day
function grd() {
grep -r -i --exclude-dir={node_modules,vendor,.git,.idea} $1 . | grep -v vendor
}
alias lsg='ls | grep'
Hook to add interesting things - at one point Ben Doherty wrapped git in timing to prove how awful our main repo was. I don’t have the timing anymore, but did get used to typing “g” instead of “git”
alias g='git'
This thing is super great. Give it a short string, and it greps your git branches for that string. If it finds exactly one match, it checks out that branch. Otherwise it tells you what it did find you can refine your query (or just copy the branch you want)
function gchlike {
local MATCHES=`g branch | grep -i $1 | awk '{print $1;}'`
local MATCHCOUNT=`echo "$MATCHES" | wc -l`
if [ "$MATCHCOUNT" -gt 1 ]; then
echo "Too many branches!"
echo "$MATCHES"
elif [ "$MATCHES" == "" ]; then
echo "No branches!"
else
echo "checking out $MATCHES"
g checkout "$MATCHES"
fi
}
Sometimes, I fuck up with git, because it’s git and it’s kinda complicated sometimes. These help recover from said fuckups.
alias unstage="g reset HEAD"
alias gpush='echo "ted you typed too fast"; g push'
alias gpull='echo "ted you typed too fast"; g pull'
If you’re in the middle of a pull or rebase and hit a conflict, this can back you out of it. The gitlab ci think wasn’t checked out by checkout * for some reason.
function idontwannamerge {
g reset HEAD;
g clean -f -d;
g checkout *
gc .gitlab-ci.yml
}
More things along the line of “oh crap I shouldn’t have committed”. Ever commit to main, push, and get the “you can’t do that” message? These are your friend.
alias undocommitanddelete="g reset HEAD^; g checkout *; g clean -f -d"
alias undocommit="g reset HEAD^"
Git add and commit with message - gotta shortcut this. Didn’t end up using `gp` as much, but oh well!
function gca(){
g add .; git commit -m "$*";
}
alias gp='g push'
alias gcf="gca 'fix'"
alias gcw="gca 'wip'"
alias gcs="gca 'squash'"
alias gcwp="gcw; gpush"
alias gcfp="gcf; gpush"
alias gcsp="gcs; gpush"
Ok so, to get a little political, sometimes there are movements to change the language we use. And sometimes, people don’t like it because changing language introduces cognitive tax, which is like, kinda understandable. And to get more political, I think it’s important to empathize with people that feel that way, even if you would prefer they change their language (which believe me, I frequently do, and in moments of impatience, wish people would just think a little harder). I do believe language shapes how we think, and changing it can change how we think, and that’s important.
This is a bit rambly, but tl;dr this all kinda manifests in this next function. I appreciate the move from master->main in git lexicon. But working in an environment that is inconsistent on which represents the “branch with the closest-to-production-code” is a frequent, albeit minor, inconvenience (i.e. a cognitive tax). So I made this function to figure it out for me.
function masterOrMain {
RESULT=`git rev-parse --verify main`
if [ -z $RESULT ];
then
echo "master"
else
echo "main"
fi
}
Checkout the right one
function gcm {
echoAndEval "g checkout `masterOrMain`"
}
Little shortcut to do all the git mechanics for finishing a merge.
function finishMerge {
g add ./
g commit --no-edit
}
alias fm='finishMerge'
Function to parse the current git branch. I totally stole this from somewhere on the internet (like any usage of sed you find in here).
function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
alias cb='parse_git_branch'
List all git branches
alias brs='git branch --sort=-authordate --format="%(HEAD)%(authordate:relative)%09%(refname:lstrip=2)"'
Git status, git checkout, git log, git log files, current branch name
alias gs='g status'
alias gc='g checkout'
alias gl='g log'
alias glf='gl --name-only'
alias glogme='gl --author=esmongeski'
alias gcp='g cherry-pick'
Git diff, git diff staged files, git diff with remote branch, git merge squash, git fetch
alias gd='g diff'
alias gds='g diff --staged'
alias gdo='g diff origin/`cb`'
alias gms='g merge --squash'
alias gf='g fetch'
This was a failed experiment to quickly switch between two git branches, typically master (nowadays main) and the current working branch. Ended up not super useful.
alias oswp="echo $OLDBRANCH; echo 'gswp to change, setswbranch to change oldbranch'"
alias swbr="echo $OLDBRANCH"
export OLDBRANCH=master
I was bitten by this once and it was a bad time. I’m not sure why it was something that would happen in my workflow, but I put this alias in to prevent me from doing it again.
alias rgi='rm .git/index.lock'
function rm {
if [ $1 == ".git/index" ]; then
echo "NOOOOOO"
else
command rm "$@"
fi
}
alias grbc='g add -uv; g rebase --continue'
alias newbr='g checkout -b'
alias unpushed='g log --branches --not --remotes'
So I haven’t used this for a while - push and immediately open the MR (PR nowadays). I should try this again.
function pushAndOpenMR {
MR_RESULT=`g push`
echo "$MR_RESULT"
findLinkAndOpen "$MR_RESULT"
}
alias gg='git grep -in'
alias setUpstreamBranch='git branch --set-upstream-to=origin/`cb` `cb`'
alias gsub=setUpstreamBranch
alias gcam='g checkout `masterOrMain` -- '
function gmm {
RESULT=`git rev-parse --verify main`
if [ -z $RESULT ];
then
echo "main is not a branch, merging master"
echoAndEval "g merge master --no-edit"
else
echo "main is a branch, merging it"
echoAndEval "g merge main --no-edit"
fi
}
function pullAndMergeMaster {
CURRENT_BRANCH=`cb`
gcm;
g pull;
gc $CURRENT_BRANCH;
gmm;
}
alias gpmm='pullAndMergeMaster'
alias delbrs='git branch | grep -v "master" | grep -v "main"" | xargs git branch -D'
alias vgi='vim .git/info/exclude'
Start an emacs daemon if one isn’t there
function ensureEmacsDaemon {
DAEMON=`ps aux | grep "emacs --daemon" | grep -v "grep"`
if [ -z "$DAEMON" ]; then
echo "no emacs daemon found - starting one"
emacs --daemon
else
echo "emacs daemon is already running"
fi
}
Start emacs as a client
alias emacs="ensureEmacsDaemon; emacsclient -t -nw"
alias killEmacs="emacsclient -e -t '(save-buffers-kill-emacs)'"
alias ke=killEmacs
Spit out the current date
alias shortdate='date +%Y-%m-%d' # get date in format YYYY-MM-DD
alias sd='shortdate'
Count the files in a given directory
function countfiles {
ls -1 $1 | wc -l | tr -d '[:space:]'
}
Echo out each line of an input
function splitOutput {
for token in $1
do
echo $token
done
}
Given a bunch of output, find anything prefixed with https and open it. This was for something specific but I don’t remember what
function findLinkAndOpen {
splitOutput "$1" | grep https | xargs open
}
Start a background ssh agent
SSH_ENV=$HOME/.ssh/environment
function start_agent {
echo "Initialising new SSH agent..."
eval /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
echo succeeded
chmod 600 ${SSH_ENV}
. ${SSH_ENV} > /dev/null
/usr/bin/ssh-add;
}
alias sag="start_agent"
# Source SSH settings, if applicable
# if [ -f "${SSH_ENV}" ]; then
# . ${SSH_ENV} > /dev/null
# ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
# start_agent;
# }
# else
# start_agent;
# fi
Aliases around cleaning up old containers
alias d='docker'
alias killcontainers='docker container stop $(docker ps -a -q)'
alias rmcontainer='d container rm -f'
alias dls='docker container ls'
alias dps='d container ls'
alias dlsa='d container ls -a'
alias rmc='rmcontainer'
alias drm='rmc'
Shortcut to bash into a container
function dbashin {
d exec -it $1 bash
}
function dshin {
d exec -it $1 sh
}
Docker rm grep - remove containers that look like a certiain thing
function drmg {
drm `dlsa | grep $1 | awk '{print $1;}'`
}
Shortcuts for interacting with pods
checkEnvAndDefault "KUBE_NAMESPACE"
function kods {
echoAndEval "kubectl get pods -n $KUBE_NAMESPACE"
}
function findpods {
echoAndEval "kubectl get pods --all-namespaces | grep $1"
}
Set and change namespace
alias skn='setkubenamespace'
function setkubenamespace {
export KUBE_NAMESPACE=$1
}
Logs and events
function klogs {
klogswithnamespace $1 $KUBE_NAMESPACE
}
function klogswithnamespace {
echoAndEval "kubectl logs $1 --namespace $2"
}
function kevs {
echoAndEval "kubectl get events -n $KUBE_NAMESPACE"
}
See k8s contexts
function kc {
echoAndEval "kubectl config get-contexts"
}
function kcsc {
echo "kubectl config use-context $1";
kubectl config use-context $1;
kc
}
Get the first column of output
function firstColumn {
awk '{print $1;}' $1;
}
If it’s there, source bash autocomplete
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
I decided to move to fish as my main shell - these are some aliases to quickly edit my fish config
alias vfp='emacs $PATH_TO_DOTFILES_REPO/fish/fishProfile.org'
All credit to Sean Ezrol for this. Script that takes an image/gif and makes it have the party colors.
function partyify {
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-i|--input-file)
INPUTFILE=$2
shift
;;
-c|-color)
COLOR=$2
shift
;;
-f|--fuzz)
FUZZ=$2
shift
;;
-o|--output-file)
OUTPUTFILE=$2
shift
;;
*)
# unknown arg
;;
esac
shift
done
echo Input - "${INPUTFILE}"
party_colors=("#93FE90" "#8FB3FC" "#CF7CFA" "#EF4CEF" "#F1586A" "#F9D48D")
for i in "${!party_colors[@]}"
do
echo magick convert "${INPUTFILE}" -fill "${party_colors[i]}" -fuzz "${FUZZ}"% -opaque "${COLOR}" party_temp-"$((i+1))".png
magick convert "${INPUTFILE}" -fill "${party_colors[i]}" -fuzz "${FUZZ}"% -opaque "${COLOR}" party_temp-"$((i+1))".png
echo Making party_temp-"$((i+1))".png, replacing "${COLOR}" with "${party_colors[i]}"
done
magick convert party_temp-%d.png[1-"${#party_colors[@]}"] -set delay 10 -loop 0 "${OUTPUTFILE}"
echo "${OUTPUTFILE} has been created."
}