-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc
211 lines (181 loc) · 5.93 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
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
#
# ~/.bashrc
#
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
if [ -d "$HOME/.bash_completion.d" ]; then
for script in $HOME/.bash_completion.d/*; do
[ -r "$script" ] && . "$script"
done
fi
# don't put duplicate lines or lines starting with space in the history.
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# Adds date to .bash_history
export HISTTIMEFORMAT="%d/%m/%y %T "
# Vim as default
export EDITOR="vim"
# Color LS output to differentiate between directories and files
export LS_OPTIONS="--color=auto"
export CLICOLOR="Yes"
export LSCOLOR=""
# Customize Path
export GOPATH="$HOME/.go"
export GOBIN="$GOPATH/bin"
export PATH=$HOME/code/monorepo/bin:$HOME/.local/bin:$GOBIN:$PATH
export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"
# https://wiki.archlinux.org/title/Docker#Rootless_Docker_daemon
export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock
if [ -f /.dockerenv ]; then
export IN_DOCKER=true
else
export IN_DOCKER=false
fi
function parse_git_branch() {
local branch
local stat
branch="$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')"
if [ ! "${branch}" == "" ]; then
stat="$(parse_git_dirty)"
echo "[${branch}${stat}]"
else
echo ""
fi
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " ${bits}"
else
echo ""
fi
}
PROMPT_COMMAND=__user_prompt_command # Func to gen PS1 after CMDs
__user_prompt_command() {
local EXIT="$?" # This needs to be first
PS1="\n"
local Red='\[\e[0;31m\]'
local Gre='\[\e[0;32m\]'
local Yel='\[\e[0;33m\]'
local Blu='\[\e[0;34m\]'
local BluBG='\[\e[48;5;27m\e[38;5;231m\]'
local GraBG='\[\e[48;5;235m\e[38;5;231m\]'
local RCol='\[\e[0m\]'
local GIT_BRANCH=$(parse_git_branch)
if [ $EXIT != 0 ]; then
PS1+="[${Red}${EXIT}${RCol}]" # Add red if exit code non 0
else
PS1+="[${Gre}${EXIT}${RCol}]"
fi
PS1+=" ${Blu}\w ${RCol}${Gre}${GIT_BRANCH}${RCol} \D{%F %T}"
if [[ $EUID -eq 0 ]]; then
PS1+="\n# "
else
PS1+="\n$ "
fi
}
# Aliases
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias gl="git log --graph --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
alias gs='git status'
alias gc='git checkout'
alias gp='git push -u'
alias gr='git reset --soft HEAD~1 && git commit --amend --no-edit'
alias gg='git log --graph --oneline --all --decorate'
alias ggm='git log --graph --oneline --decorate origin/master HEAD'
alias gd="git diff $(git merge-base origin/master HEAD) --name-only"
alias gb='git for-each-ref --sort=-committerdate refs/heads/'
alias gt='git log --no-walk --tags --pretty="%h %d %s" --decorate=full'
alias grep='grep --color=auto'
alias kbdoff="sudo sys76-kb set -b 0"
alias ls='ls --color=auto'
alias ll='ls -l'
alias power="upower -i /org/freedesktop/UPower/devices/battery_BAT0"
alias stop="pkill -STOP"
alias resume="pkill -CONT"
alias hostname="cat /etc/hostname"
alias vim="nvim"
alias zwift="DONT_CHECK=true CONTAINER_TOOL='sudo docker' zwift"
# Functions
function home() {
export home="$PWD"
}
function cd() {
HOME="${home:=$HOME}" builtin cd "$@"
}
function rgplace() {
if [[ $# -lt 2 ]]; then
echo "Usage: rgplace <search_pattern> <replacement> [file_pattern]"
echo "Example: rgplace 'foo' 'bar' '*.txt'"
return 1
fi
local search_pattern=$1
local replacement=$2
local file_pattern=$3
if [ -z "$file_pattern" ]; then
file_pattern="*"
fi
rg --files -g "$file_pattern" | while read -r file; do
if rg -q "$search_pattern" "$file"; then
sed -i "s/${search_pattern}/${replacement}/g" "$file"
fi
done
}
function ga() {
local message="$1"
if [ -z "$message" ]; then
>&2 echo "Commit message is required."
return 2
fi
git commit --amend -m "${message}"
}
function gsp() {
local subtree="${1:-}"
local toplevel
toplevel=$(git rev-parse --show-toplevel)
if [ -z "$subtree" ]; then
>&2 echo "Missing argument 'subtree'."
echo "Pick one of:"
# https://stackoverflow.com/a/18339297
git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2 | sort | uniq | xargs -I {} bash -c 'if [ -d $(git rev-parse --show-toplevel)/{} ] ; then echo " {}"; fi'
return 2
fi
git -C "$toplevel" subtree push --prefix "$subtree" "[email protected]:jmelahman/$(basename "${subtree}").git" master
}
# Name the current terminal tab in xterm-compatible program.
# See also, https://github.com/lanoxx/tilda/issues/134#issuecomment-419906171
function title() { echo -e "\e]2;${1}\a tab --> [${1}]"; }
eval $(ssh-agent) > /dev/null