-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc
executable file
·179 lines (152 loc) · 5.07 KB
/
.bashrc
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
function print_ssh_ps1()
{
if [ -n "${SSH_CLIENT}" ] || [ -n "${SSH_TTY}" ]; then
printf "\e[31m@%s\e[0m" "$(hostname)"
fi
}
function print_git_branch_ps1()
{
local BRANCH_NAME
BRANCH_NAME="$(git branch --show-current 2> /dev/null)"
if [[ -n "${BRANCH_NAME}" ]]; then
local BGCOL="\e[43;30m"
if [[ -z "$(git status 2>/dev/null | grep 'Changes not staged for commit')" ]]; then
BGCOL="\e[42;30m"
else
BGCOL="\e[43;30m"
fi
printf "\x01${BGCOL}\x02 ${BRANCH_NAME} \x01\e[0m\x02"
fi
}
PS1='\[\e]0;\007\]' # set window title
PS1="${PS1} " # <space>
PS1="${PS1}\u" # user
PS1="${PS1}\$(print_ssh_ps1)" # bash function
PS1="${PS1} " # <space>
PS1="${PS1}\[\e[44;30m\]" # begin blue background, black foreground
PS1="${PS1} " # <space>
PS1="${PS1}\w" # current working directory
PS1="${PS1} " # <space>
PS1="${PS1}\[\e[0m\]" # end blue background, black foreground
PS1="${PS1}\$(print_git_branch_ps1)" # bash function
PS1="${PS1}\[\e[0m\]" # clear color
PS1="${PS1} " # <space>
export PS1
function rgrep()
{
# TODO: need to change the line number format to: +<num> vs :<num>
# this is for each vim support to jump to the correct line number after a grep
local TARGET_DIR_REGEX
local TARGET_DIR
local S_DIR
TARGET_DIR_REGEX=$(if [ -z "${2}" ]; then echo "."; else echo "${2}"; fi)
IFS="*" read -r -a TARGET_DIR <<<"${TARGET_DIR_REGEX}"
S_DIR=$(if [ -z "${TARGET_DIR[0]}" ]; then echo "."; else echo "${TARGET_DIR[0]}"; fi)
# TODO: can't use 'script' as it isn't cross platform
local GREP_OUT
GREP_OUT="$(script -q /dev/null grep -iInr --include="*${TARGET_DIR[1]##*.}" "${1}" "${S_DIR}" \
--color=auto --exclude-dir={build,.git,node_modules,deps,assets} | sed 's/\.\/*//')"
echo "${GREP_OUT}"
}
function rfind()
{
local TARGET_DIR
TARGET_DIR="${2}"
if [[ -z "${TARGET_DIR}" ]]; then
TARGET_DIR="."
fi
find "${TARGET_DIR}" -iname "*${1}*";
}
function setup_ssh_agent()
{
mkdir -p "${HOME}/.ssh"
SSH_AGENT_TIMEOUT=9000 # 2.5 hours
SSH_ENV="${HOME}/.ssh/agent.env"
# setup environment
test -r "${SSH_ENV}" && eval "$(<${SSH_ENV})" >/dev/null;
AGENT_RUN_STATE=$(ssh-add -l >/dev/null 2>&1; echo ${?})
if [ ! "${SSH_AUTH_SOCK}" ] || [ ${AGENT_RUN_STATE} = 2 ]; then
(umask 066; ssh-agent > "${SSH_ENV}")
eval "$(<${SSH_ENV})" >/dev/null;
# assumes name of key is id_rsa
ssh-add -q -t ${SSH_AGENT_TIMEOUT} >/dev/null
elif [ ! "${SSH_AUTH_SOCK}" ] || [ ${AGENT_RUN_STATE} = 1 ]; then
ssh-add -q -t ${SSH_AGENT_TIMEOUT} >/dev/null
fi
}
# If running a git command for the first time, setup ssh-agent
function g()
{
# TODO: need to only run when git would normally request a password (not on git status)
setup_ssh_agent
git "${@}"
}
function tag()
{
if [[ "${OSTYPE}" == *"msys"* ]]; then
if [[ -z $(which mintty.exe 2>/dev/null) ]]; then
return
fi
fi
local TARGET_DIR
TARGET_DIR="${@}"
if [[ -z "${TARGET_DIR}" ]]; then
TARGET_DIR="."
fi
find "${TARGET_DIR}" -type f -iregex '.*\.\(h\|hpp\|c\|cpp\|hxx\)$' | xargs -d '\n' ctags -a
}
function ropen()
{
local FILE_PATH
FILE_PATH="${@////\\}"
if [[ -z ${FILE_PATH} ]]; then
FILE_PATH="$(pwd)"
FILE_PATH="${FILE_PATH////\\}"
fi
if [[ "${OSTYPE}" == *"msys"* ]]; then
# Replace slashes with backslashes.
# If the argument starts with "/c/" or "\c\", replace with "C:\".
explorer.exe $(echo ${FILE_PATH} | sed 's/\/c\//C:\\/' | sed 's/\\c\\/C:\\/')
else
open "${@}"
fi
}
function goog()
{
if [[ "${OSTYPE}" == *"msys"* ]]; then
start "https://www.google.com/search?q=$(echo ${@} | sed 's/ /+/g')"
else
echo "TODO: fix me"
fi
}
if [[ "${OSTYPE}" == "darwin"* ]]; then
LSCOLORS="Gxfxcxdxbxegedabagacad"
export LSCOLORS
alias ls="ls -h -G"
else
alias ls="ls -h -G --color=auto"
fi
alias la="ls -a"
alias ll="ls -l"
alias ..="cd .."
alias ...="cd ../.."
alias wc="wc -l"
# TODO: the grep/find stuff is so complicated...
# I'm sure there's something simpler I could do
alias _grep_common="grep -iI --color=auto -d skip"
alias xgrep="xargs grep -iIr --color=auto"
#alias rgrep="ls | xargs grep -iIr --color=auto"
alias vim='vim --noplugin -u ${HOME}/.vimrc'
alias edf='cd . && cd ${HOME}/.vim/sessions && vim && cd - >/dev/null'
alias sdf='cd . && source ${HOME}/.bashrc && cd - >/dev/null'
alias diff="diff -Bd -U 5 --color=auto"
shopt -s checkwinsize
shopt -s histappend
HISTCONTROL=ignoredups:erasedups
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
export HISTSIZE=100000
export HISTFILESIZE=${HISTSIZE}
export TERM=xterm-256color
if [[ -f "${HOME}/.bashrc.local" ]]; then
source "${HOME}/.bashrc.local"
fi