alias shorten=”a loooong, very often used command”
How to create your own bash aliases for productivity, and my entire list of them.
Less keystrokes, more productivity.
I live inside the CLI most of the time and one of the main pain points for me, is mistyping a part of a command, and then having to either left-arrow it, or retype it. This soon becomes irritating, specially when you’r on a roll.
My logical conclusion was that less keystrokes = less chances to mistype.
My next step was to procrastinate for a little, and save in the long run.
I started out with git.
Place the following code in your ~/.bash_profile and type source ~/.bash_profile . Then you can use gits on the command line like any other bash command
Keep doing this with the aliases below to have them available too.
alias gits="git status"
This worked amazing ! It’s really hard to mistype gits and it’s insanely fast to type !
Next, git add
Because I don’t have trigger finger, that dd really messed with me.
alias gita="git add"
Until I ended up with this
alias gits=”git status”
alias gitr=”git reset HEAD”
alias gitd=”git diff”
alias gitdc=”git diff — cached”
alias gita=”git add”
alias gitaa=”git add .”
alias gitc=”git commit -S -m”
alias gitcc=”git checkout”
alias gitp=”git push origin”
alias gitt=”git tag -s $1"
alias gittv=”git tag -v $1"
Worked like a charm.
I then started working with Kubernetes. We had quite a few contexts at the time and to keep typing kubectl config use-context [context] was just ridiculous. So I made another alias.
alias kucon="kubectl config use-context"
I started forgetting the names of the contexts after a while away, and getting them made me sleepy by the end.
alias kucons="kubectl config get-contexts"
If you start using Kubernetes and need to debug, you need to be able to find pods. kubectl get pods
alias kupods="kubectl get pods"
If your cluster is used by many teams, you’ll probably have a bunch of namespaces
alias kuns="kubectl get namespaces"
You know that thing that happens when you call kubectl get pods , then have to re-do that with kubectl get pods -n project-x-dev because you’re in the wrong namespace ?
Did you know you can switch the namespace you currently use ?
kubectl config set-context $(kubectl config current-context) --namespace=project-x-dev
NOPE ! Not typing that ! Like operating heavy machinery under the influence !
function kuswitchns {
kubectl config set-context $(kubectl config current-context) --namespace="$1";
}
# Export exports the function above as a bash function
export -f kuswitchns
Now i can just kuswitchns to project-x-dev and forget about that pesky little -n thing !
Until it ended up like this
alias kucon="kubectl config use-context"
alias kucons="kubectl config get-contexts"
alias kupods="kubectl get pods"
alias kuns="kubectl get namespaces"
alias kurs="kubectl get rs"
alias kudel="kubectl delete"
alias kuapp="kubectl apply"
alias kudes="kubectl describe"
alias kulog="kubectl logs"
alias kupf="kubectl port-forward"
function kuswitchns { kubectl config set-context $(kubectl config current-context) --namespace="$1"; }
export -f kuswitchns
Did you know you can choose columns when you call docker ps ? I do this by default, as my terminal keeps wrapping the long names & ports and it gets hard to read.
alias dls='docker ps --format "table {{.ID}} {{.Names}}\t{{.Ports}}"'
This is specially handy when in a CI process and the log are exposed through a tiny window in a browser 👌🏼
You know that feeling when you create a directory, and then have to retype it and cd into it ?!
function mkcdir { mkdir -p $1; cd $1; }
export -f mkcdir
You know that thing where you google base64 encoder and go to the first google result, paste, copy, paste ?
Why not just use the command line ? Specially when your cli is on a hotkey !
echo -n "aliases are magic" | base64
> YWxpYXNlcyBhcmUgbWFnaWM=
and if you want to get the value back again ?
echo -n 'YWxpYXNlcyBhcmUgbWFnaWM=' | base64 -D
> aliases are magic
I dont like typing pipes followed by 4 letter commands !
function b64 { echo -n "$1" | base64; }
function b64d { echo -n "$1" | base64 -D; }
export -f b64
export -f b64d
Now it’s just
b64 'aliases are amazing'
> YWxpYXNlcyBhcmUgYW1hemluZw==
b64d 'YWxpYXNlcyBhcmUgYW1hemluZw=='
> aliases are amazing
You need to have fun with them too !
Laravel ?! no … parti
########################################
# Laravel Artisan
########################################
alias parti="php artisan"
Django ?! no … pyman
########################################
# Django Manage
########################################
alias pyman="python manage.py"
########################################
# Virtual Env
########################################
alias virenv="virtualenv env --python python3.6 && source env/bin/activate"
Oh yes ! When my bash alias file became a bit larger, I started adding those nice separators to see exactly what was going on.
It does take some time to type those hashes though !
function new_alias_group {
echo '########################################';
echo "# $1";
echo '########################################';
}
export -f new_alias_group
$ new_alias_group 'Docker Commands'
########################################
# Docker Commands
########################################
The biggest command in my bash shortcuts ?
alias gitpretty="git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all"
The command above was stolen from somewhere that I cannot remember. But it prints out git branches and how they where merged etc. on the cli. It’s pretty sick !
OH and an entertaining one too !
A few things I have learnt using so many aliases
- When pair programming, remember to use the full commands
- When presenting, remember to use the full commands
- When ssh’ing into a server, remember to use the full commands
- Use the full commands a few times before aliasing them, else you’ll forget the full command
- Be careful of adding them in scripts for CI / CD
- Make sure to use unique commands so as to not override others
- Back them up in Github or the like, so your new machine can immediately be a productivity machine !
I truly hope you have learnt something from this post !
Whether its aliases, how to write a bash function and export it, that you can switch your default Kubernetes namespace, that you can specify go templates for many docker commands, given you an idea for a new productivity command, or reassured you that if you can add stickers on your machine and make it yours, then surely you can make your shell yours too !
Happy Bashing !