forked from AdamWhittingham/bash-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_adam
247 lines (204 loc) · 5.92 KB
/
bash_adam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Command Aliases
alias psg='ps -ef|grep '
alias sshp='ssh -o"PreferredAuthentications=password"'
alias ack='ack-grep'
## ls aliases
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias lh='ls -alFh'
## apt-get aliases
alias sai='sudo apt-get install'
alias sar='sudo apt-get remove'
alias saar='sudo apt-get autoremove'
alias sau='sudo apt-get update'
alias saup='sudo apt-get upgrade'
## Ruby aliases
alias be='bundle exec'
alias cucumber='bundle exec cucumber --color'
alias guard='bundle exec guard'
alias rackup='bundle exec rackup'
alias rake='bundle exec rake'
alias rspec='bundle exec rspec --color'
## Git aliases
alias g='git'
alias ga='git add'
alias gaa='git add -A'
alias gb='git blame -CCC'
alias gc='git commit'
alias gcaa='git commit -a --amend -CHEAD'
alias gd='git diff'
alias gdw='git diff --word-diff'
alias gcl='git clean -fd'
alias gco='git checkout'
alias gl='git_pretty_log'
alias gld='git log -p'
alias gls='git shortlog -sn'
alias gst='git status -sb'
## Dev aliases
alias prp='git pull && rake && git push'
alias filecomp='file_composition'
## tmux aliases
alias tmux='tmux -2' #launch tmux in 256 colours
alias tl='tmux ls'
alias tls='tmux ls'
alias tm='tmux_create_or_attach'
alias tma='tmux_new_attach_to_session'
## Proxy aliases
alias p='source $HOME/.bash-config/proxy on'
alias np='source $HOME/.bash-config/proxy off'
## Super lazy-time update functions
alias up-vim='git-update $HOME/.vim'
alias up-bash='git-update $HOME/.bash-config'
alias up-tmux='git-update $HOME/.tmux-config'
alias up-all='up-vim && up-bash && up-tmux'
# Bind extra keys
## Bind UpArrow and DownArrow to search to autocomplete the current command
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
## Bind PgUp and PgDown to scoll through history
bind '"\e[5~": previous-history'
bind '"\e[6~": next-history'
# Configure extra things
## Extra Git config
git config --global alias.meld !~/.bash-config/git-meld.pl
git config --global branch.autosetuprebase always
git config --global color.ui auto
git config --global push.default current
#Tmux Functions
function tmux_create_or_attach(){
session="$1"
if [[ ! $(tmux has-session -t "$session" &>/dev/null) ]] ; then
tmux new -s "$session" -d
fi
tmux attach -t "$session"
}
function tmux_new_attach_to_session(){
session="$1"
counter=0
while (tmux has-session -t "${session}_${counter}") ; do
counter=$[$counter +1]
echo $counter
done
tmux new-session -s "${session}_${counter}" -t "${session}"
}
# Extra Useful Functions
function file_composition {
find . -type f | perl -n -e'/(\.\w+$)/ && print "$1\n"' | sort | uniq -c | sort -n
}
function source_if_present(){
local file="$1"
[[ -f $file ]] && source "$file"
}
function git-update {
local updir=$1
echo "git pull (in $updir)"
if pushd $updir >/dev/null ; then
git pull
popd >/dev/null
fi
}
function seconds_to_time_str {
local secs="$1"
printf "%dh %dm %ds" $((${secs}/3600)) $((${secs}%3600/60)) $((${secs}%60)) | grep -o "[1-9].*$"
}
function seconds_to_minutes {
local secs="$1"
echo "$((${secs}/60))"
}
# Colour Vars
bold="$(tput bold)"
reset="\001$(tput sgr0)\002"
blue="\001$bold$(tput setaf 4)\002"
cyan="\001$bold$(tput setaf 6)\002"
green="\001$bold$(tput setaf 2)\002"
purple="\001$bold$(tput setaf 5)\002"
red="\001$bold$(tput setaf 1)\002"
white="\001$bold$(tput setaf 7)\002"
yellow="\001$bold$(tput setaf 3)\002"
# Prompt Colour Functions
function user_colour {
local colour=$green
if [[ $(id -u) == 0 ]]; then
colour=$red
fi
echo $colour
}
function colour_for_minutes {
local minutes="$1"
local colour=$green
if [[ $minutes -gt 30 ]]; then colour=$red
elif [[ $minutes -gt 15 ]]; then colour=$yellow
fi
echo -e "$colour"
}
# Git Prompt Niceness
function git_working_changed {
git diff --quiet 2> /dev/null
if [[ $? == 0 ]]; then return 1; else return 0; fi
}
function git_staging_changed {
git diff --cached --quiet 2> /dev/null
if [[ $? == 0 ]]; then return 1; else return 0; fi
}
function git_changed {
$(git_working_changed) || $(git_staging_changed)
}
function git_branch {
echo "$(__git_ps1 "%s")"
}
function git_seconds_since_commit {
now=`date +%s`
local last_commit=`git log --pretty=format:'%at' -1`
local seconds=$((now-last_commit))
echo $seconds
}
function git_mins_since_commit {
echo "$(seconds_to_minutes $(git_seconds_since_commit))"
}
function git_branch_colour {
local colour=$blue
if $(git_changed) ; then
local minutes_since_commit="$(git_mins_since_commit)"
colour=$(colour_for_minutes $minutes_since_commit)
fi
echo -e $colour
}
git_prompt() {
if [ -n "$(__gitdir)" ]; then
echo -e "$(git_branch_colour)($(git_branch))"
fi
}
# Git Log Niceness
_I_=' '
HASH="%C(green)%h%C(reset)"
AGE="%C(yellow)%ar%C(reset)"
AUTHOR="%C(bold blue)%an%C(reset)"
REFS="%C(bold red)%d%C(reset)"
COMMENT="%s"
FORMAT="$HASH$_I_$AGE$_I_$AUTHOR$_I_$REFS $COMMENT"
git_pretty_log() {
git log --graph --decorate --pretty="tformat:${FORMAT}" $* |
less -FXRS
}
# Load Extra Things
source_if_present "$HOME/.rvm/scripts/rvm"
source_if_present "$HOME/.bash-config/git-completion.sh"
source_if_present "$HOME/.bash-config/git-prompt.sh"
## Setup Proxy if set in profile.d
source_if_present "/etc/profile.d/proxy"
# Setup iTerm2 tab names
export PROMPT_COMMAND='echo -ne "\\033]0;$(basename ${PWD/#$HOME/~})\\007"'
# Git 'g' alias auto-completion fixes
bashdefault -o default -o nospace -F _git g 2>/dev/null || complete -o default -o nospace -F _git g
__git_complete g __git_main
# Force term type to stop all the stupidity in vim, tmux et. al. Not a good idea but fixes too many things not to do it for now.
export TERM=xterm-256color
# Get CLI file colors back in OS X
export CLICOLOR=1
# Setup Prompt
export PS2="-> "
export PS1="$(user_colour)\u$reset@$yellow\h$reset:$purple\W$reset\$(git_prompt)$reset$PS2"
function check_shellshock(){
env X="() { :;} ; echo You are vulnerable to shellshock! Update bash!" `which bash` -c ''
}