willpower232

willpower232 / bash.bashrc

Last active 1 month ago

Like 0

I am making my .bashrc portable as inspired by this https://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html

Revision 60b0998b50fe85ec11184f6fe4bd783ae3d7c518

bash.bashrc Raw
1#### Set prompt basics ####
2
3# set variable identifying the chroot you work in (used in the prompt below)
4if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
5 debian_chroot=$(cat /etc/debian_chroot)
6fi
7
8# set a fancy prompt (non-color, unless we know we "want" color)
9case "$TERM" in
10 xterm-color) color_prompt=yes;;
11esac
12
13# uncomment for a colored prompt, if the terminal has the capability; turned
14# off by default to not distract the user: the focus in a terminal window
15# should be on the output of commands, not on the prompt
16force_color_prompt=yes
17
18if [ -n "$force_color_prompt" ]; then
19 if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
20 # We have color support; assume it's compliant with Ecma-48
21 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
22 # a case would tend to support setf rather than setaf.)
23 color_prompt=yes
24 else
25 color_prompt=
26 fi
27fi
28
29#### Set initial prompt ####
30
31ps1_shelllevel='${debian_chroot:+($debian_chroot)}($SHLVL) '
32
33if [ "$color_prompt" = yes ]; then
34 ps1_user='\u@\h {\A} '
35
36 # have to remember to wrap the 0-width characters on the line of the prompt in \[ and \]
37 if [ "$EUID" -ne 0 ]; then
38 ps1_folder='\e[32m[\w]'
39 ps1_prompt='\n\[\e[0m\]$ '
40 else
41 ps1_folder='\e[31m[\w]'
42 ps1_prompt='\n\[\e[0m\]# '
43 fi
44else
45 ps1_user='u@h {a} '
46 ps1_folder='[w]'
47
48 if [ "$EUID" -ne 0 ]; then
49 ps1_prompt='\n$ '
50 else
51 ps1_prompt='\n# '
52 fi
53fi
54
55PS1=$ps1_shelllevel$ps1_user$ps1_folder$ps1_prompt
56
57# don't unset these so they can be used later
58#unset color_prompt force_color_prompt
59
60# not sure what this bit does
61#case "$TERM" in
62# xterm*|rxvt*)
63# PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1";;
64# *)
65# ;;
66#esac
67
68#### Colour support ####
69
70if [ -x /usr/bin/dircolors ]; then
71 test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
72 alias ls='ls --color=auto'
73 alias dir='dir --color=auto'
74 alias vdir='vdir --color=auto'
75
76 alias grep='grep --color=auto'
77 alias fgrep='fgrep --color=auto'
78 alias egrep='egrep --color=auto'
79
80 # may be --color=auto in some versions...
81 alias ip='ip -c'
82fi
83
84#### Aliases for all ####
85
86# alias commands that prefix commands
87# so they can prefix your aliases
88# https://askubuntu.com/a/22043
89alias sudo='sudo '
90alias watch='watch '
91
92alias vim='vim -i NONE'
93alias nano='nano --nowrap --smooth -T 4'
94alias plz='sudo '
95alias dammit='sudo !!'
96
97# stop yourself from breaking the server by accident
98alias mv='mv -i'
99alias cp='cp -i'
100alias ln='ln -i'
101alias rm='rm -I --preserve-root'
102alias chown='chown --preserve-root'
103alias chmod='chmod --preserve-root'
104alias chgrp='chgrp --preserve-root'
105
106# I haven't used these in a while since I taught myself to use grep -R
107# don't forget about -i for ignoring case and -l if you just want file names and first matches
108# but you could rewrite these to use ag if you wanted but they also might not follow symlinks
109#function findinfiles() { find ./ -type f -print0 | xargs -0 grep -i "$1"; }
110#function findinphpfiles() { find ./ -type f -name "*.php" -print0 | xargs -0 grep -i "$1"; }
111
112complete -W "$(echo $(grep '^ssh ' ~/.bash_history | sort -u | sed 's/^ssh //'))" ssh
113
114if [ "$(uname)" == "Darwin" ]; then
115 # got pbcopy
116 :
117elif grep -q Microsoft /proc/version; then
118 # route error to /dev/null because thanks microsoft
119 alias pbcopy='clip.exe > /dev/null 2>&1'
120 # todo: pbpaste in wsl
121else
122 # install xclip through apt first
123 alias pbcopy='xclip -selection clipboard'
124 alias pbpaste='xclip -selection clipboard -o'
125fi
126
127# inspired by @jamesdoc https://twitter.com/jamesdoc/status/902829327187931137
128# ref https://github.com/dannyfritz/commit-message-emoji
129alias gc-InitialCommit="printf \"๐ŸŽ‰\" | pbcopy && pbpaste && echo \"\""
130alias gc-VersionTag="printf \"๐Ÿ”–\" | pbcopy && pbpaste && echo \"\""
131alias gc-NewFeature="printf \"โœจ\" | pbcopy && pbpaste && echo \"\""
132alias gc-Bugfix="printf \"๐Ÿ›\" | pbcopy && pbpaste && echo \"\""
133alias gc-SecurityFix="printf \"๐Ÿ”’\" | pbcopy && pbpaste && echo \"\""
134alias gc-Metadata="printf \"๐Ÿ“‡\" | pbcopy && pbpaste && echo \"\""
135alias gc-Refactoring="printf \"โ™ป๏ธ\" | pbcopy && pbpaste && echo \"\""
136alias gc-Documentation="printf \"๐Ÿ“š\" | pbcopy && pbpaste && echo \"\""
137alias gc-Internationalization="printf \"๐ŸŒ\" | pbcopy && pbpaste && echo \"\""
138alias gc-Accessibility="printf \"โ™ฟ\" | pbcopy && pbpaste && echo \"\""
139alias gc-Performance="printf \"๐ŸŽ\" | pbcopy && pbpaste && echo \"\""
140alias gc-Cosmetic="printf \"๐ŸŽจ\" | pbcopy && pbpaste && echo \"\""
141alias gc-Tooling="printf \"๐Ÿ”ง\" | pbcopy && pbpaste && echo \"\""
142alias gc-Tests="printf \"๐Ÿšจ\" | pbcopy && pbpaste && echo \"\""
143alias gc-Deprecation="printf \"๐Ÿ’ฉ\" | pbcopy && pbpaste && echo \"\""
144alias gc-Removal="printf \"๐Ÿ—‘๏ธ\" | pbcopy && pbpaste && echo \"\""
145alias gc-WorkInProgress="printf \"๐Ÿšง\" | pbcopy && pbpaste && echo \"\""
146alias gc-Deforestation="printf \"๐ŸŒฒ\" | pbcopy && pbpaste && echo \"\""
147
148if [ -x /usr/bin/termsaver ]; then
149 alias tsm="termsaver matrix"
150fi
151
152if [ -x /usr/bin/cmatrix ]; then
153 alias etm="cmatrix -a -b -s -C cyan"
154fi
155
156#### Final settings for all ####
157
158# this is needed to enter the passphrase when attempting to gpg-sign git commits
159#export GPG_TTY=$(tty)
160
161# if you're on WSL you may want to see information from MOTD such as pending updates
162#sudo run-parts /etc/update-motd.d/
163
164# if you're on WSL and you want to mount network shares
165#sudo mount -a
166
167# if you want to hide commands from your bash history with space and don't include duplicates
168# you need this but it may already be set elsewhere
169#HISTCONTROL=ignoreboth
170
171# just in case you're interested in history, this should include when the command was run
172HISTTIMEFORMAT="%F %T "
173
174# append history list to history file when shell exits, default?
175shopt -s histappend
176# append every command to the history list
177PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
178
179# Debian doesn't have bash-completion by default so you need to apt install bash-completion and add the below
180# also don't forget about https://github.com/cykerway/complete-alias
181#if [ -f /etc/bash_completion ]; then
182# . /etc/bash_completion
183#fi
184
185# set PATH so it includes user's private bin if it exists
186# I use a separate bin to backup standalone binaries
187#if [ -d "$HOME/bin" ] ; then
188# PATH="$HOME/bin:$PATH"
189#fi
190
my.bashrc Raw
1# don't need to do this
2# /etc/bash.bashrc
3
4# simple function to output yay or nay based on last status code
5# have to include it directly in PS1 so it reads the right status code
6# PROMPT_COMMAND can't see it
7echo_last_status() {
8 if [ $? = 0 ]; then
9 echo "๐Ÿ‘Œ"
10 else
11 echo "๐Ÿ‘Ž"
12 fi
13 # newlines are not visible
14}
15
16# git clone git@github.com:jimeh/git-aware-prompt ~/.bash/git-aware-prompt
17git_branch=
18git_dirty=
19if [ -d ~/.bash/git-aware-prompt ]; then
20 source ~/.bash/git-aware-prompt/prompt.sh
21fi
22
23if [ "$EUID" -ne 0 ]; then
24 if [ "$color_prompt" = yes ]; then
25 ps1_folder='\e[32m[\w] \e[1;34m$git_branch\e[31m$git_dirty'
26 else
27 ps1_folder='[w] $git_branch$git_dirty'
28 fi
29fi
30
31PS1='$(echo_last_status)\n'$ps1_shelllevel$ps1_user$ps1_folder$ps1_prompt
32
33# if [[ "$TERM" == "xterm-256color" && "$SHLVL" == "1" ]]; then
34# echo ""
35# curl --silent --max-time 2 https://wttr.in/lincoln,uk?0Q
36
37# if [[ $(which tailscale) != "" ]]; then
38# expiryEpoch=$(date -d $(tailscale status --json | jq -r '.Self.KeyExpiry') +%s)
39# nowEpoch=$(date +%s)
40
41# if [[ $(($expiryEpoch - $nowEpoch)) -lt $((86400 * 3)) ]]; then
42# echo -e "tailscale key expiring soon, time to \e[38;5;208mtailscale up --force-reauth\e[0m but not over ssh, obviously"
43# fi
44# fi
45# fi
46
47# lol
48alias nano='vim'
49
50# alias bashphp='docker exec -it -w "$PWD" phpfpm bash'
51# alias bashnode='bash ~/containers/nodejs/interactive.sh'
52# alias bashgolang='bash ~/containers/golang/interactive.sh'
53# alias bashruby='bash ~/containers/ruby/interactive.sh'
54# alias bashpython='bash ~/containers/python/interactive.sh'
55
56function git {
57 if [[ "$1" == "clone" && "$@" != *"--help"* ]]; then
58
59 # I prefer to not include file permissions when cloning
60 # unfortunately this is more involved than just the below
61 # command git clone --config core.filemode=false "$@"
62
63 curdir="$(pwd)"
64
65 argsarray=($(echo $@ | tr " " "\n"))
66 lastarg=${argsarray[-1]}
67 penultimatearg=${argsarray[-2]}
68
69 # if the penultimate arg contains @ or :// then it must be the repo url
70 if ! case $penultimatearg in *"@"* | *"://"*) false;; esac; then
71 newdir="$lastarg"
72 else
73 newdir="$(basename $lastarg .git)"
74 if case $penultimatearg in *":"*) false;; esac; then
75 newdir=${newdir#*:}
76 fi
77 fi
78
79 # this original way which doesn't support args when clone'ing
80 #if [ -z "$3" ]; then
81 # newdir="$(basename $2 .git)"
82 #else
83 # newdir="$3"
84 #fi
85
86 shift 1
87 command git clone "$@" || return 1
88 cd $newdir
89
90 #case $@ in
91 # *"workgithubname"* | *"WorkGithubName"* | *"othercompanyname"*)
92 # command git config user.email me@work.com && echo "set user for work boss"
93 # ;;
94
95 # *"personalgithubname"* | *"gist.github.com"*)
96 # command git config user.email me@home.com && echo "set user for personal boss"
97 # ;;
98
99 # *)
100 # echo "whoareyou"
101 # ;;
102 #esac
103
104 command git config --unset core.filemode && echo "removed core.filemode setting boss"
105 cd $curdir
106
107 elif [[ "$1" == "branch" && "$@" != *"--help"* && "$@" != *" -r"* && -s "$2" && -z "$3" ]]; then
108 if [[ "$2" == "--random" ]]; then
109 command git fetch --all;
110 while true; do name=$RANDOM; if [ -z "$(command git show-ref refs/heads/$name)" ]; then command git checkout -b $name; break; fi; done;
111 else
112 shift 1
113 echo "switching to checkout..."
114 command git checkout -b "$@" || command git checkout "$@"
115 fi
116
117 else
118 command git "$@"
119 fi
120}
121
122alias g=git
123complete -F _complete_alias g
124alias n=nautilus
125
126function docker {
127 if [[ "$1" == "stop-and-rm" ]]; then
128 shift 1
129 command docker stop "$@"
130 command docker rm "$@"
131 elif [[ "$1" == "login-to-github" ]]; then
132 echo 'ghp_abcd' | docker login ghcr.io -u youruser --password-stdin
133 else
134 command docker "$@"
135 fi
136}
137
138alias d=docker
139complete -F _complete_alias d
140
141# aliases let you put things after the command if you want
142# i.e. get familiar with the flags but type way fewer characters
143# e.g. dcb --no-cache -> rebuild the images without cache
144# e.g. dcu -d -> dc up and detach/background it
145# e.g. dce app bash -> dc exec bash in the service
146# e.g. dcd -v -> dc down and clear volumes too
147
148export DOCKER_BUILDKIT=1
149alias dc="docker-compose"
150#alias dcb="docker-compose build --parallel"
151alias dcb="docker-compose build"
152alias dcd="docker-compose down"
153alias dcu="docker-compose up"
154#alias dce="docker-compose exec --user $(id -u):$(id -g) --env HOME=/tmp"
155alias dce="docker-compose exec"
156
157alias dceanrb="dce app npm run build"
158alias dceanrd="dce app npm run dev"
159alias dceanrw="dce app npm run watch"
160alias dceapat="dce app php artisan tinker"
161alias dceapa="dce app php artisan"
162alias dceact="dce app composer test"
163alias dceaca="dce app composer analyse"
164alias dcetct="dce testapp composer test"
165alias dcetca="dce testapp composer analyse"
166alias dcetact="dce test-app composer test"
167
168# persisting the changes you've made to your container
169# is a terrible idea and yet here it is
170function dcc {
171 if [ "$#" -ne "1" ]; then
172 echo "Precisely one argument required"
173 return 1
174 fi
175
176 CONTAINER=$(dc images | grep "_$1 " | awk '{print $1;}')
177 TAGNAME=$(dc images | grep "_app " | awk '{print $2;}')
178 TAGVERSION=$(dc images | grep "_app " | awk '{print $3;}')
179
180 if [ "$CONTAINER" = "" ] || [ "$TAGNAME" = "" ] || [ "$TAGVERSION" = "" ]; then
181 echo "Unable to figure out what to do"
182 echo "you will need to dcu -d again if you have already run this command"
183 return 1
184 fi
185
186 COMMAND="docker commit $CONTAINER $TAGNAME:$TAGVERSION"
187 echo $COMMAND
188 eval $COMMAND
189}
190
191# function k8sprod {
192# bash --rcfile ~/.bashrc.k8s
193# }
194
195alias tf="terraform"
196
my.bashrc.k8s Raw
1if [ -f "$HOME/.bashrc" ]; then
2 . "$HOME/.bashrc"
3fi
4
5cd ~/gitrepos/k8s-configs
6
7# https://github.com/ahmetb/kubectx
8k8s_context=
9k8s_namespace=
10find_k8s() {
11 k8s_context="$(kubectx -c)"
12 k8s_namespace="$(kubens -c)"
13}
14
15PROMPT_COMMAND="find_k8s; $PROMPT_COMMAND"
16
17if [ "$EUID" -ne 0 ]; then
18 if [ "$color_prompt" = yes ]; then
19 ps1_folder='\e[32m[\w] \e[1;34m$k8s_context/$k8s_namespace'
20 else
21 ps1_folder='[w] $k8s_context/$k8s_namespace'
22 fi
23fi
24
25PS1='$(echo_last_status)\n'$ps1_shelllevel$ps1_user$ps1_folder$ps1_prompt
26
27export AWS_PROFILE=k8s
28export AWS_SDK_LOAD_CONFIG=1
29export KOPS_STATE_STORE=your-kops-state-store
30
31source <(kubectl completion bash)
32
33# have to manually install other bash completions
34# cd $(pkg-config --variable=completionsdir bash-completion)
35# curl https://raw.githubusercontent.com/ahmetb/kubectx/master/completion/kubectx.bash | sudo tee kubectx
36# curl https://raw.githubusercontent.com/ahmetb/kubectx/master/completion/kubens.bash | sudo tee kubens
37
38alias k=kubectl
39complete -F __start_kubectl k
40#complete -F _complete_alias k
41
42function qk {
43 if [[ "$1" == "token" ]]; then
44 command aws-iam-authenticator token -i your.k8s.cluster -r arn:aws:iam::012345678910:role/OrganizationAccountAccessRole | jq -r .status.token | pbcopy
45 elif [[ "$1" == "dashboard" ]]; then
46 echo "Copying token to clipboard..."
47 qk token
48 echo "Done"
49 echo "When running, visit http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/"
50 k proxy
51 elif [[ "$1" == "watch" ]]; then
52 shift 1
53 if [[ "$1" == "get" ]]; then
54 shift 1
55 k get --watch "$@"
56 else
57 command watch -n 1 kubectl "$@"
58 fi
59 elif [[ "$1" == "podexec" ]]; then
60 shift 1
61 firstpod="$(k get pods --output=jsonpath="{.items[0].metadata.name}" | head -1)"
62 echo "Running in $firstpod"
63 k exec -it $firstpod "$@"
64 elif [[ "$1" == "redeploy" ]]; then
65 shift 1
66 k patch deployment "$1" -p "{\"spec\": {\"template\": {\"metadata\": { \"labels\": { \"redeploy\": \"$(date +%s)\"}}}}}"
67 else
68 command kubectl "$@"
69 fi
70}
71
72alias kbuild='kustomize build --enable-alpha-plugins .'
my.profile Raw
1# if running bash
2if [ -n "$BASH_VERSION" ]; then
3 # include .bashrc if it exists
4 if [ -f "$HOME/.bashrc" ]; then
5 . "$HOME/.bashrc"
6 fi
7fi
8
9# likely to not have a UI so need to sign commits in the terminal
10#export GPG_TTY=$(tty)
11