forked from acornejo/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh.alias
219 lines (202 loc) · 5.13 KB
/
sh.alias
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
#!/bin/sh
# aliases: save typing
alias ll="ls -l $LS_OPTIONS"
alias l="ll -h"
alias dir="ls -1"
alias pk="pkill -9 -f"
alias v="$EDITOR"
alias vi="$EDITOR"
alias vim="$EDITOR"
alias g="git"
# aliases: default options
alias rm="rm -i"
alias ping="ping -c4 -t4"
alias route="route -n"
alias netstat="netstat -n"
alias bc="bc -q -l"
alias du="du -h -s -c *"
# aliases: useful extras
alias trunc='cut -c -${COLUMNS:-80}'
alias sidebyside='diff -W ${COLUMNS:-80} -y'
# aliases: ack/ag
if hash ack-grep 2>/dev/null; then
alias ack="ack-grep"
fi
if hash rg 2>/dev/null; then
alias a="rg"
elif hash ag 2>/dev/null; then
alias a="ag"
else
alias a="ack"
fi
# aliases: genot
if hash genot 2>/dev/null; then
alias tip='genot -c ~/.tips'
fi
# aliases: todo.sh
if hash todo.sh 2>/dev/null; then
alias t='todo.sh'
fi
# display short pwd
shortpwd() {
local PRE= NAME="$PWD" LENGTH=${1:-20}
[[ "$NAME" != "${NAME#$HOME/}" || -z "${NAME#$HOME}" ]] &&
PRE+='~' NAME="${NAME#$HOME}" LENGTH=$[LENGTH-1];
((${#NAME}>$LENGTH)) && NAME="/...${NAME:$[${#NAME}-LENGTH+4]}";
echo "$PRE$NAME"
}
check_fzf() {
if ! hash fzf 2>/dev/null; then
echo "fzf not found!"
return 1
else
return 0
fi
}
check_z() {
if (type z | grep 'not found' >/dev/null 2>&1); then
echo "z not found!"
return 1
else
return 0
fi
}
check_in_git_repo() {
if ! git rev-parse HEAD > /dev/null 2>&1; then
echo "not in git repo!"
return 1
else
return 0
fi
}
# file finder
f() {
if hash ag 2>/dev/null; then
local match=$1
shift
ag -l -i --nocolor -g "$match" $@
else
find . -iname "*$1*" -not \( -path '*/.git' -o -path '*/.svn' -o -path '*/.hg' -prune \)
fi
}
# file browsing
fv() {
check_fzf || return 1
if [ -z "$FV_PER_DIR" ]; then
local selected=$(fzf)
if [ -f "$selected" ]; then
${EDITOR:-echo} $selected
fi
else
local dir=${1:-.}
local selected=$(ls "$dir" | fzf)
if [ -n "$selected" ]; then
if [ -d "$dir/$selected" ]; then
fv "$dir/$selected"
else
${EDITOR:-echo} "$dir/$selected"
fi
fi
fi
}
# directory browsing
fcd() {
check_fzf && check_z || return 1
local dir="$(z -l | awk '/^[0-9]+.*\// {print $2}' | fzf --tac)"
test -t 1 && cd $dir || echo "cd $dir"
}
# git branch browser
gb() {
check_fzf && check_in_git_repo || return 1
local FMT="%C(reset)%C(blue)%cd %C(green)%h %C(reset)%s %C(yellow)(%cn)"
local branches=$(git for-each-ref refs/heads --sort=-authordate | cut -d/ -f3)
local branch=$(echo "$branches" | fzf --ansi --no-sort --reverse \
--header "current branch: $(git rev-parse --abbrev-ref HEAD)" \
--tiebreak=index \
--preview "git log --graph --date=short --format='$FMT' {} | head -$LINES")
if [ $? -ne 0 ]; then
return
fi
if [ -t 1 ]; then
git checkout $branch
else
echo $branch
fi
}
# git diff browser
gd() {
check_fzf && check_in_git_repo || return 1
(
cd $(git rev-parse --show-toplevel)
while true; do
local RES=$(git status --porcelain | \
fzf --ansi --nth 2 --no-sort --reverse --tiebreak=index \
--bind=ctrl-s:toggle-sort \
--header "branch: $(git rev-parse --abbrev-ref HEAD)" \
--preview 'git diff --color=always -- $(echo {} | grep -o "[^ ]*$")' \
--expect 'ctrl-m,ctrl-a,ctrl-d' | tail -n2)
local KEY=$(echo "$RES" | head -n1)
local STATUS=$(echo "$RES" | tail -n1 | awk '{print $1}')
local FILE=$(echo "$RES" | tail -n1 | awk '{print $NF}')
case $KEY in
ctrl-m)
if [ "$STATUS" == "M" ]; then
git diff -- $FILE
else
less $FILE
fi
;;
ctrl-a)
git add -- $FILE
;;
ctrl-d)
git reset -q -- $FILE
;;
*)
exit
esac
done
)
}
# git log browser
gl() {
check_fzf && check_in_git_repo || return 1
local FMT="%C(reset)%C(blue)%cd %C(green)%h %C(reset)%s %C(yellow)(%cn)"
local LN=$LINES
local HEADER="branch: $(git rev-parse --abbrev-ref HEAD) args: $(echo $@)"
git log --graph --color=always --date=short --format="$FMT" "$@" | \
if [ -t 1 ]; then
fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
--header "$HEADER" \
--preview "echo {} | grep -o '[a-f0-9]\{7\}' -m 1 |
xargs -I % sh -c 'git show --color=always % | head -$LN '" \
--bind "enter:execute:echo {} | grep -o '[a-f0-9]\{7\}' -m 1 |
xargs -I % sh -c 'git show --color=always %'"
else
fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
--header "$HEADER" \
--preview "echo {} | grep -o '[a-f0-9]\{7\}' -m 1 |
xargs -I % sh -c 'git show --color=always % | head -$LN '" \
| grep -o '[a-f0-9]\{7\}' -m 1
fi
}
# open files
open() {
local open_cmd=""
case "$OSTYPE" in
darwin*)
open_cmd="open"
;;
linux*)
open_cmd="xdg-open"
;;
cygwin*)
open_cmd="cygstart"
;;
*)
echo "Platform $OSTYPE unsupported"
return 1
;;
esac
nohup $open_cmd "$@" >/dev/null 2>&1
}