-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbash_includes
93 lines (84 loc) · 2.71 KB
/
bash_includes
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
###
### General bash configuration, to include in osx or linux servers
###
export EDITOR=vim
HISTFILESIZE=100000
HISTSIZE=20000
export HISTCONTROL=ignoredups
export HISTCONTROL=ignoreboth # ignore same sucessive entries
shopt -s histappend
PROMPT_COMMAND='history -a'
# up arrow bash history
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
###
### Prompt
###
# Show current git project info
parse_git_dirty() {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working tree clean" ]] && echo "*"
}
parse_git_branch() {
git branch 2> /dev/null | sed -e "/^[^*]/d" -e "s/* \(.*\)/(\1$(parse_git_dirty))/"
}
current_virtualenv() {
echo "$(basename $(dirname $VIRTUAL_ENV 2>/dev/null) 2>/dev/null)/$(basename $VIRTUAL_ENV 2>/dev/null)"
}
current_kubectl_context() {
echo "$(kubectl config current-context 2>/dev/null)"
}
current_kubectl_context_namespace() {
echo "$(kubectl config view --minify --output 'jsonpath={..namespace}' 2>/dev/null || echo 'N/A')"
}
function __prompt_command() {
local LAST_EXIT_CODE="$?"
### Define Colors
txtblack='\[\033[0;30m\]'
txtred='\[\033[0;31m\]'
txtgreen='\[\033[0;32m\]'
txtorange='\[\033[0;33m\]'
txtblue='\[\033[0;34m\]'
txtpurple='\[\033[0;35m\]'
txtwhite='\[\033[0;37m\]'
txtend='\[\033[0m\]'
### Define PS1
# extra vertical space
PS1="\n"
# username @ hostname, then indicate previous exit code
PS1+="${txtgreen}\u$DIM${txtend} ${txtwhite}@${txtend} ${txtorange}\h${txtend}"
# kubectl context, if we're in one
if [ -n "$(current_kubectl_context)" ]
then
PS1+=" ${txtpurple}kube:$(current_kubectl_context):$(current_kubectl_context_namespace)${txtend}"
fi
# python virtualenv, if we're in one
if [ -n "$(current_virtualenv)" ]
then
PS1+=" ${txtblue}($(current_virtualenv))${txtend}"
fi
# sun or rain based on last exit code
if [ $LAST_EXIT_CODE = 0 ]
then
PS1+=" ${txtgreen}☀${txtend}"
else
PS1+=" ${txtblue}☁${txtend}"
fi
PS1+="\n"
# pwd, git branch and prompt
PS1+="${txtpurple}\$PWD${txtend} ${txtwhite}\$(parse_git_branch)$ "
}
export PROMPT_COMMAND=__prompt_command
###
### useful aliases
###
alias lsl='ls -G -lh --color=auto'
alias grep='grep -i --color=auto'
alias gg='git grep -n --color --heading --break'
# tmux 256 colors hack
alias tmux="TERM=screen-256color-bce tmux"
# rename the current tab in terminal/iterm2
rn() { export PROMPT_COMMAND="echo -ne \"\033]0;$1\007\""; }
# create an empty new bash script
generatebashscript() { touch $1; chmod a+x $1; echo -e "#!/bin/bash\n\n" > $1; vim $1; }
# convert a simple yaml file to json
yaml_file_as_json() { ruby -e 'require "yaml"; require "json"; puts JSON.dump(YAML.parse(open(File.expand_path("'"$1"'")).read).to_ruby)'; }