-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc.global
338 lines (306 loc) · 10.9 KB
/
.bashrc.global
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_TYPE=en_US.UTF-8
export SVN_EDITOR=vim
export EDITOR=vim
export TERM=xterm-256color
# Common functions {{{ #
vimgdb() {
args=""
for arg in $@; do
args=$args""$arg" "
done
cmd="vim -c \"VBGstartGDB $args\" -c \"only\" ."
eval $cmd
}
isAndroidOS() {
if [[ `uname` != 'Darwin' ]]; then
if [[ `uname -o` == 'Android' ]]; then
true
else
false
fi
else
false
fi
}
isMacOS() {
if [[ `uname` != 'Darwin' ]]; then
false
else
true
fi
}
# Install function {{{ #
function install_tool() {
github_repo=$1
tool_name=$2
tool_version=$3
full_name=${4:-$tool_name}
TOOL_BIN=$LOCAL_BIN/$tool_name
if [ ! -f $TOOL_BIN ]; then
if [[ `uname` != 'Darwin' ]]; then
export archive_name=$full_name'-'$tool_version'-x86_64-unknown-linux-musl'
else
export archive_name=$full_name'-'$tool_version'-x86_64-apple-darwin'
fi
tmp_dir='/tmp/'
if isAndroidOS; then
tmp_dir='~/tmp/'
mkdir -p $tmp_dir
fi
wget -O $tmp_dir/$archive_name.tar.gz $github_repo/releases/download/$tool_version/$archive_name.tar.gz
tar xf $tmp_dir/$archive_name.tar.gz -C $tmp_dir
cp -f $tmp_dir/$archive_name/$tool_name $TOOL_BIN
echo "$tmp_dir/$archive_name"
fi
}
# }}} Install function #
# }}} Common functions #
# Aliases {{{ #
if ! isMacOS; then
alias ls='ls --color=always'
else
alias ls='ls -G'
fi
if isAndroidOS; then
alias grep='grep'
else
alias grep='grep --color'
fi
alias gitv='vim "$(git rev-parse --show-toplevel)"/.git/index -c "Gitv" -c "tabonly"'
alias google-chrome='google-chrome --proxy-auto-detect'
alias whatsapp='google-chrome --app=https://web.whatsapp.com/' #'/opt/google/chrome/google-chrome --app=https://web.whatsapp.com/'
#alias vim='vim --startuptime vim.log'
# }}} Aliases #
# Setup environment {{{ #
LOCAL_BIN=${HOME}/.bin/
mkdir -p $LOCAL_BIN
export PATH=/opt/homebrew/bin/:$PATH
export PATH=$LOCAL_BIN:$PATH
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
# Install ripgrep {{{ #
TOOL_BIN=$LOCAL_BIN/rg
if [ ! -f $TOOL_BIN ]; then
echo "Installing ripgrep"
res=$(install_tool 'https://github.com/BurntSushi/ripgrep' 'rg' '13.0.0' 'ripgrep')
# Enable autocompletion
cp -f $res/complete/rg.bash $BASH_COMPL_DIR
cp -f $res/complete/_rg $ZSH_COMPL_DIR
fi
# }}} Install ripgrep
# }}} Setup environment #
# FZF configurations {{{ #
# FZF search program. Get config from here: https://medium.com/@crashybang/supercharge-vim-with-fzf-and-ripgrep-d4661fc853d2
# --files: List files that would be searched but do not search
# --no-ignore: Do not respect .gitignore, etc...
# --hidden: Search hidden files and folders
# --follow: Follow symlinks
# --glob: Additional conditions for search (in this case ignore everything in the .git/ folder)
export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow --glob "!.git/*"'
export FZF_DEFAULT_OPTS='--height 30% --border'
##########################################################################
### FZF CONFIGS
##########################################################################
# fe [FUZZY PATTERN] - Open the selected file with the default editor
# - Bypass fuzzy finder if there's only one match (--select-1)
# - Exit if there's no match (--exit-0)
fe() {
local files
IFS=$'\n' files=($(fzf-tmux --preview '[[ $(file --mime {}) =~ binary ]] && \
echo {} is a binary file || \
(highlight -O ansi -l {} || \
coderay {} || \
rougify {} || \
cat {}) 2> /dev/null | head -500' --query="$1" --multi --select-1 --exit-0))
[[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}"
}
# fcd - including hidden directories
fcd() {
local dir
local file
file=$(locate $(pwd) | fzf -e -q "$*")
if [ ! -d "$file" ]; then
file=$(dirname "$file")
fi
cd "$file"
}
# fcdh - cd from home
fcdh() {
local dir
local file
file=$(locate ~ | fzf -e -q "$*")
if [ ! -d "$file" ]; then
file=$(dirname "$file")
fi
cd "$file"
}
# fcdr - cd to directory from root
fcdr() {
local dir
local file
file=$(locate / | fzf -e -q "$*")
if [ ! -d "$file" ]; then
file=$(dirname "$file")
fi
cd "$file"
}
# fk - kill process
fk() {
local pid
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
if [ "x$pid" != "x" ]
then
echo $pid | xargs kill -${1:-9}
fi
}
# Check if you in git repository
is_in_git_repo() {
git rev-parse HEAD > /dev/null 2>&1
}
# Show current git status with diff
gdiff() {
is_in_git_repo || return
git -c color.status=always status --short |
fzf -m --ansi --nth 2..,.. \
--preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1}) | head -500' |
cut -c4- | sed 's/.* -> //'
}
# gcol - checkout git branch (locals)
gcol() {
is_in_git_repo || return
local branches branch
branches=$(git branch -vv) &&
branch=$(echo "$branches" | grep -v '/HEAD\s' | fzf +m --ansi --multi --tac --preview-window right:50% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##') &&
git checkout $(echo "$branch" | awk '{print $1}' | sed "s/.* //")
}
# gcor - checkout git branch (including remote branches)
gcor() {
is_in_git_repo || return
local branches branch
branches=$(git branch --all | grep -v HEAD) &&
branch=$(echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m --ansi --multi --tac --preview-window right:50% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##') &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
}
# gcort - checkout remote git branch with track option (including remote branches)
gcort() {
is_in_git_repo || return
local branches branch
branches=$(git branch --all | grep -v HEAD) &&
branch=$(echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m --ansi --multi --tac --preview-window right:50% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##') &&
git checkout --track $(echo "$branch" | sed "s/.* //")
}
# gcot - checkout git branch/tag
gcot() {
is_in_git_repo || return
local tags branches target
tags=$(git tag --sort -version:refname) || return
branches=$(
git branch --all | grep -v HEAD |
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
sort -u | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
target=$(
(echo "$tags"; echo "$branches") |
fzf-tmux -- --no-hscroll --ansi +m --multi --preview-window right:70% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" {} | head -'$LINES -d "\t" -n 2) || return
git checkout $(echo "$target" | awk '{print $2}')
}
# gcoc - checkout git commit
gcoc() {
is_in_git_repo || return
local commits commit
commits=$(git log --pretty=oneline --abbrev-commit --reverse) &&
commit=$(echo "$commits" | fzf --tac +s +m -e --multi --preview-window right:50% \
--preview 'git show --color=always $(sed "s/ .*//" <<< {}) | head -'$LINES) &&
git checkout $(echo "$commit" | sed "s/ .*//")
}
# gdlb - delete local branch
gdlb() {
is_in_git_repo || return
local branches branch
branches=$(git branch -vv) &&
branch=$(echo "$branches" | fzf +m --ansi --multi --tac --preview-window right:50% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##') &&
git branch -D $(echo "$branch" | awk '{print $1}' | sed "s/.* //")
}
# gdrb - delete remote branch
gdrb() {
is_in_git_repo || return
local branches branch remote_name branch_name
remote_name=""
branches=$(git branch --remotes | grep -v HEAD) &&
branch=$(echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m --ansi --multi --tac --preview-window right:50% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##')
IFS='/' read remote_name branch_name <<< "$branch"
echo "git push ${remote_name} --delete ${branch_name}"
git push ${remote_name} --delete ${branch_name}
}
# gshow - git commit browser
gshow() {
is_in_git_repo || return
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
--bind "ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
{}
FZF-EOF" \
--preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES
}
# tm - connect to tmux session
tm() {
local session
session=$(tmux list-sessions -F "#{session_name}" | \
fzf --query="$1" --select-1 --exit-0) &&
tmux attach-session -t "$session" || echo "No tmux sessions"
}
# tk - kill tmux session
tk() {
local session
session=$(tmux list-sessions -F "#{session_name}" | \
fzf --query="$1" --exit-0) &&
tmux kill-session -t "$session" || echo "No tmux sessions"
}
# gitvl - Show local branch in gitv
gitvl() {
is_in_git_repo || return
local branches branch
branches=$(git branch -vv) &&
branch=$(echo "$branches" | grep -v '/HEAD\s' | fzf +m --ansi --multi --tac --preview-window right:50% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##') &&
vim "$(git rev-parse --show-toplevel)"/.git/index -c "Gitv "$(echo "$branch" | awk '{print $1}' | sed "s/.* //") -c "tabonly"
}
# gitvr - Show remote branch in gitv
gitvr() {
is_in_git_repo || return
local branches branch
branches=$(git branch --all | grep -v HEAD) &&
branch=$(echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m --ansi --multi --tac --preview-window right:50% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##') &&
vim "$(git rev-parse --show-toplevel)"/.git/index -c "Gitv "$(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##") -c "tabonly"
}
# }}} FZF configurations #
export GPG_TTY=$(tty)