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 3f350ddfe41bf88f810016fe1a3ab99e1ae4c28e

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
106function findinfiles() { find ./ -type f -print0 | xargs -0 grep -i "$1"; }
107function findinphpfiles() { find ./ -type f -name "*.php" -print0 | xargs -0 grep -i "$1"; }
108
109complete -W "$(echo $(grep '^ssh ' ~/.bash_history | sort -u | sed 's/^ssh //'))" ssh
110
111if [ "$(uname)" == "Darwin" ]; then
112 # got pbcopy
113 :
114elif grep -q Microsoft /proc/version; then
115 # route error to /dev/null because thanks microsoft
116 alias pbcopy='clip.exe > /dev/null 2>&1'
117 # todo: pbpaste in wsl
118else
119 # install xclip through apt first
120 alias pbcopy='xclip -selection clipboard'
121 alias pbpaste='xclip -selection clipboard -o'
122fi
123
124# inspired by @jamesdoc https://twitter.com/jamesdoc/status/902829327187931137
125# ref https://github.com/dannyfritz/commit-message-emoji
126alias gc-InitialCommit="printf \"๐ŸŽ‰\" | pbcopy && pbpaste && echo \"\""
127alias gc-VersionTag="printf \"๐Ÿ”–\" | pbcopy && pbpaste && echo \"\""
128alias gc-NewFeature="printf \"โœจ\" | pbcopy && pbpaste && echo \"\""
129alias gc-Bugfix="printf \"๐Ÿ›\" | pbcopy && pbpaste && echo \"\""
130alias gc-SecurityFix="printf \"๐Ÿ”’\" | pbcopy && pbpaste && echo \"\""
131alias gc-Metadata="printf \"๐Ÿ“‡\" | pbcopy && pbpaste && echo \"\""
132alias gc-Refactoring="printf \"โ™ป๏ธ\" | pbcopy && pbpaste && echo \"\""
133alias gc-Documentation="printf \"๐Ÿ“š\" | pbcopy && pbpaste && echo \"\""
134alias gc-Internationalization="printf \"๐ŸŒ\" | pbcopy && pbpaste && echo \"\""
135alias gc-Accessibility="printf \"โ™ฟ\" | pbcopy && pbpaste && echo \"\""
136alias gc-Performance="printf \"๐ŸŽ\" | pbcopy && pbpaste && echo \"\""
137alias gc-Cosmetic="printf \"๐ŸŽจ\" | pbcopy && pbpaste && echo \"\""
138alias gc-Tooling="printf \"๐Ÿ”ง\" | pbcopy && pbpaste && echo \"\""
139alias gc-Tests="printf \"๐Ÿšจ\" | pbcopy && pbpaste && echo \"\""
140alias gc-Deprecation="printf \"๐Ÿ’ฉ\" | pbcopy && pbpaste && echo \"\""
141alias gc-Removal="printf \"๐Ÿ—‘๏ธ\" | pbcopy && pbpaste && echo \"\""
142alias gc-WorkInProgress="printf \"๐Ÿšง\" | pbcopy && pbpaste && echo \"\""
143alias gc-Deforestation="printf \"๐ŸŒฒ\" | pbcopy && pbpaste && echo \"\""
144
145if [ -x /usr/bin/termsaver ]; then
146 alias tsm="termsaver matrix"
147fi
148
149if [ -x /usr/bin/cmatrix ]; then
150 alias etm="cmatrix -a -b -s -C cyan"
151fi
152
153#### Final settings for all ####
154
155# this is needed to enter the passphrase when attempting to gpg-sign git commits
156#export GPG_TTY=$(tty)
157
158# if you're on WSL you may want to see information from MOTD such as pending updates
159#sudo run-parts /etc/update-motd.d/
160
161# if you're on WSL and you want to mount network shares
162#sudo mount -a
163
164# if you want to hide commands from your bash history with space and don't include duplicates
165# you need this but it may already be set elsewhere
166#HISTCONTROL=ignoreboth
167
168# Debian doesn't have bash-completion by default so you need to apt install bash-completion and add the below
169# also don't forget about https://github.com/cykerway/complete-alias
170#if [ -f /etc/bash_completion ]; then
171# . /etc/bash_completion
172#fi
173
174# set PATH so it includes user's private bin if it exists
175# I use a separate bin to backup standalone binaries
176#if [ -d "$HOME/bin" ] ; then
177# PATH="$HOME/bin:$PATH"
178#fi
179
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
6echo_last_status() {
7 if [ $? = 0 ]; then
8 echo "๐Ÿ‘Œ"
9 else
10 echo "๐Ÿ‘Ž"
11 fi
12 # can't output a newline
13}
14
15# git clone git@github.com:jimeh/git-aware-prompt ~/.bash/git-aware-prompt
16git_branch=
17git_dirty=
18if [ -d ~/.bash/git-aware-prompt ]; then
19 source ~/.bash/git-aware-prompt/prompt.sh
20fi
21
22if [ "$EUID" -ne 0 ]; then
23 if [ "$color_prompt" = yes ]; then
24 ps1_folder='\e[32m[\w] \e[1;34m$git_branch\e[31m$git_dirty'
25 else
26 ps1_folder='[w] $git_branch$git_dirty'
27 fi
28fi
29
30PS1='$(echo_last_status)\n'$ps1_shelllevel$ps1_user$ps1_folder$ps1_prompt
31
32# lol
33alias nano='vim'
34
35# alias bashphp='docker exec -it -w "$PWD" phpfpm bash'
36# alias bashnode='bash ~/containers/nodejs/interactive.sh'
37# alias bashruby='bash ~/containers/ruby/interactive.sh'
38# alias bashpython='bash ~/containers/python/interactive.sh'
39
40function git {
41 if [[ "$1" == "clone" && "$@" != *"--help"* ]]; then
42
43 # I prefer to not include file permissions when cloning
44 # unfortunately this is more involved than just the below
45 # command git clone --config core.filemode=false "$@"
46
47 curdir="$(pwd)"
48
49 argsarray=($(echo $@ | tr " " "\n"))
50 lastarg=${argsarray[-1]}
51 penultimatearg=${argsarray[-2]}
52
53 # if the penultimate arg contains @ or :// then it must be the repo url
54 if ! case $penultimatearg in *"@"* | *"://"*) false;; esac; then
55 newdir="$lastarg"
56 else
57 newdir="$(basename $lastarg .git)"
58 fi
59
60 # this original way which doesn't support args when clone'ing
61 #if [ -z "$3" ]; then
62 # newdir="$(basename $2 .git)"
63 #else
64 # newdir="$3"
65 #fi
66
67 shift 1
68 command git clone "$@" || return 1
69 cd $newdir
70
71 #case $@ in
72 # *"workgithubname"*)
73 # command git config user.email me@work.com
74 # echo "set user for work boss"
75 # ;;
76
77 # *"personalgithubname"*)
78 # command git config user.email me@home.com
79 # echo "set user for personal boss"
80 # ;;
81
82 # *)
83 # echo "whoareyou"
84 # ;;
85 #esac
86
87 command git config --unset core.filemode
88 echo "removed core.filemode setting boss"
89 cd $curdir
90
91 elif [[ "$1" == "branch" && "$@" != *"--help"* && -z "$3" ]]; then
92 shift 1
93 echo "switching to checkout..."
94 command git checkout -b "$@" || command git checkout "$@"
95 else
96 command git "$@"
97 fi
98}
99
100alias g=git
101complete -F _complete_alias g
102alias n=nautilus
103
104function docker {
105 if [[ "$1" == "stop-and-rm" ]]; then
106 shift 1
107 command docker stop "$@"
108 command docker rm "$@"
109 else
110 command docker "$@"
111 fi
112}
113
114alias d=docker
115complete -F _complete_alias d
116
117# aliases let you put things after the command if you want
118# i.e. get familiar with the flags but type way fewer characters
119# e.g. dc build --no-cache -> rebuild the images without cache
120# e.g. dcu -d -> dc up and detach/background it
121# e.g. dce app bash -> dc exec bash in the service
122# e.g. dcd -v -> dc down and clear volumes too
123
124alias dc=docker-compose
125alias dcd="docker-compose down"
126alias dcu="docker-compose up"
127#alias dce="docker-compose exec --user $(id -u):$(id -g) --env HOME=/tmp"
128alias dce="docker-compose exec"
129
130# persisting the changes you've made to your container
131# is a terrible idea and yet here it is
132function dcc {
133 if [ "$#" -ne "1" ]; then
134 echo "Precisely one argument required"
135 return 1
136 fi
137
138 CONTAINER=$(dc images | grep "_$1 " | awk '{print $1;}')
139 TAGNAME=$(dc images | grep "_app " | awk '{print $2;}')
140 TAGVERSION=$(dc images | grep "_app " | awk '{print $3;}')
141
142 if [ "$CONTAINER" = "" ] || [ "$TAGNAME" = "" ] || [ "$TAGVERSION" = "" ]; then
143 echo "Unable to figure out what to do"
144 echo "you will need to dcu -d again if you have already run this command"
145 return 1
146 fi
147
148 COMMAND="docker commit $CONTAINER $TAGNAME:$TAGVERSION"
149 echo $COMMAND
150 eval $COMMAND
151}
152
153#function k8sprod {
154# bash --rcfile ~/.bashrc.k8s
155#}
156
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
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