-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_functions
executable file
·303 lines (223 loc) · 5.69 KB
/
.bash_functions
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
# vim: ft=sh
# colored less
c() {
pygmentize -O style=monokai -f console256 -g $1 | less -NRFXS
}
# Ex) grepr before after dir1/ dir2/
grepr() {
BEFORE=$1
AFTER=$2
shift
shift
grep $* -rle "${BEFORE}" | xargs sed -i -e "s/${BEFORE}/${AFTER}/g"
}
if [ $(uname | grep -qe 'NT') ]; then
wincmd() {
CMD=$1
shift
$CMD $* 2>&1 | j2u
}
fi
r() {
if [ -z "$RANGER_LEVEL" ]; then
ranger $@
else
exit
fi
}
mkcd() {
DIRNAME=$1
mkdir $DIRNAME && cd $DIRNAME
}
# function using fuzzy finder
export FUZZY_FINDER_COMMAND="fzy --show-scores --line=20"
export FUZZY_FINDER_COMMAND_WITH_QUERY="fzy --show-scores --line=20 --query="
# make user choise one from candidate list and return it
__fuzzy() {
CANDIDATE_FILEPATH="$1"
QUERY="$2"
COMMAND_WITH_QUERY="$FUZZY_FINDER_COMMAND_WITH_QUERY"$QUERY""
COMMAND=
if [ "x$QUERY" == "x" ]; then
COMMAND="$FUZZY_FINDER_COMMAND"
else
COMMAND="$COMMAND_WITH_QUERY"
fi
RESULT="$(cat $CANDIDATE_FILEPATH | $COMMAND)"
if [ "x$RESULT" != "x" -a "x$RESULT" != "xcat $CANDIDATE_FILEPATH | $COMMAND" ]; then
echo $RESULT
fi
}
f-cd() {
QUERY="$1"
TEMPFILE=$(mktemp)
trap 'rm -f $TEMPFILE; echo aaaa' EXIT PIPE TERM INT
# create candidate list and write it into tempfile
find . -type d | grep -v .cache | grep -v .git | grep -v vendor > $TEMPFILE
# item chosen by user
CHOSEN="$(__fuzzy "$TEMPFILE" "$QUERY")"
if [ "x$CHOSEN" != "x" ]; then
cd "$CHOSEN"
else
echo "operation was cancelled."
fi
}
f-vf() {
QUERY="$1"
TEMPFILE=$(mktemp)
trap 'rm -f $TEMPFILE; echo aaaa' EXIT PIPE TERM INT
# create candidate list and write it into tempfile
find . -type f | grep -v .git | grep -v cache > $TEMPFILE
# item chosen by user
CHOSEN="$(__fuzzy "$TEMPFILE" "$QUERY")"
if [ "x$CHOSEN" != "x" ]; then
$EDITOR "$CHOSEN"
else
echo "operation was cancelled."
fi
}
f-gl() {
git ls-files | $FUZZY_FINDER_COMMAND
}
f-vgl() {
FILE="$(f-gl)"
if [ "x$FILE" != "x" ]; then
$EDITOR "$FILE"
else
echo "operation was cancelled."
fi
}
f-gc() {
BRANCH_LIST="$(git branch -a --format='%(refname:lstrip=2)' | sed -e 's/^origin\///g' | sort -u | $FUZZY_FINDER_COMMAND)"
trap 'rm -f $TEMPFILE; echo aaaa' EXIT PIPE TERM INT
if [ "x$BRANCH_LIST" != "x" ]; then
git checkout "$BRANCH_LIST"
else
echo "operation was cancelled."
fi
}
f-h() {
QUERY="$1"
TEMPFILE=$(mktemp)
# create candidate list and write it into tempfile
find ~/wk/projects/bw/ -maxdepth 1 -type d > $TEMPFILE
# item chosen by user
CHOSEN="$(__fuzzy "$TEMPFILE" "$QUERY")"
if [ "x$CHOSEN" != "x" ]; then
cd "$CHOSEN"
else
echo "operation was cancelled."
fi
}
f-vl() {
QUERY="$1"
TEMPFILE=$(mktemp)
trap 'rm -f $TEMPFILE; echo aaaa' EXIT PIPE TERM INT
# create candidate list and write it into tempfile
find ~/.vim/dein/repos/github.com/ -maxdepth 2 -type d > $TEMPFILE
# item chosen by user
CHOSEN="$(__fuzzy "$TEMPFILE" "$QUERY")"
if [ "x$CHOSEN" != "x" ]; then
cd "$CHOSEN"
else
echo "operation was cancelled."
fi
}
f-hist() {
tempfile=$(mktemp)
tmux popup -E " tac ~/.bash_history | sed -e '/^#[0-9]\+$/d' | uniq | $FUZZY_FINDER_COMMAND > $tempfile"
CHOSEN=$(head -1 $tempfile)
if [ "x$CHOSEN" != "x" ]; then
echo -e "\e[1mselected:\e[0m \e[36;3m${CHOSEN}\e[0m"
history -s "$CHOSEN"
else
echo "operation was cancelled."
fi
trap 'rm -f $tempfile; echo aaaa' EXIT PIPE TERM INT
}
ee() {
w3m http://googledictionary.freecollocation.com/meaning?word=${1// /+}
}
kv() {
ps aux \
| grep vim \
| head -3 \
| sed -e 's/^\w\+\s\+\(\w\+\).*/\1/g' \
| xargs -rp kill -s 9
}
mine() {
chown $(whoami):$(whoami) $*
}
gongkai() {
chmod 777 $*
}
# ls with less
lll() {
ls -hlA --color=always $* | less -RNSJFX
}
llal() {
ls -hla --color=always $* | less -RNSJFX
}
sshinit() {
pkill ssh-agent
ps aux | grep ssh-agent | grep -v grep > /dev/null 2>&1
if [ ! $? = 0 ]; then
eval `ssh-agent`
fi
wait
ssh-add ~/.ssh/id_rsa \
&& ssh -T [email protected] \
&& ssh -T [email protected]
}
release_note() {
RELEASE_NOTE_FILENAME=release_$(basename $(pwd))_$(date +%Y.%m.%d_%T).log
git co master && git pull \
&& git co develop && git pull \
&& git log --oneline master..develop --merges > $RELEASE_NOTE_FILENAME \
&& vim $RELEASE_NOTE_FILENAME -c new -c GBrowse
}
lsbinds() {
bind -p \
| grep -av '#' \
| grep -av self-insert \
| grep -av do-lowercase-version \
| grep -av '\[\S' \
| cut -d: -f1,2 \
| sed 's/[":]//g' \
| sed 's/\\e/M-/g' \
| xargs printf "%-10s - %s\n" \
| less -NF
}
ring_start() {
local osc="]440;Ring10.wav:loop"
__run_on_tmux
if [ $? = 0 ]; then
echo $osc
else
__tmux_escape $osc
fi
}
ring_stop() {
local osc="]440;"
__run_on_tmux
if [ $? = 0 ]; then
echo $osc
else
__tmux_escape $osc
fi
}
__run_on_tmux() {
if [ "x$TMUX" == "x" ]; then
return 0
else
return 1
fi
}
__tmux_escape() {
echo "Ptmux;${1//}\\"
}
port_is_busy() {
local port=$1
lsof -i:$port 1> /dev/null 2>&1
if [ "x$?" == "x1" ] ; then echo free; else echo busy; fi
}