-
Notifications
You must be signed in to change notification settings - Fork 0
/
.func.zsh
205 lines (189 loc) · 4.04 KB
/
.func.zsh
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
# shell functions
lc(){
if (( $# == 0 ));
then
ls | wc -l
else
ls "$1" | wc -l
fi
}
lac(){
if (( $# == 0 ));
then
echo $[ `la | wc -l` - 1 ]
else
echo $[ `la "$1" | wc -l` - 1 ]
fi
}
up(){
local old=`pwd`
for i in $(seq "${1:-1}"); do
old="${old%/*}"
if [ -z "$old" ]; then
break
fi
done
if [ -z "$old" ]; then
old='/'
fi
cd "$old"
}
mkcd(){
mkdir -vp $1 && cd $1
}
dumpinto(){
if (( $# == 0 )); then
echo Need one argument
return 1
fi
if (( $# > 1 )); then
echo Too many arguments
return 1
fi
if [ ! -d "$1" ]; then
mkdir "$1"
fi
if (( `ls "$1" | wc -l` > 0 )); then
echo Directory "$1" exists and not empty
return 1
fi
ls -A | grep -v "^$1$" | xargs -I file mv file "$1"
}
swap(){
if (( $# != 2 )) ; then
echo "Exact 2 command line arguments needed!" >&2
return 1
fi
local tmpnames=(tmp.$$ .tmp.$$ tmp temp .tmp .temp)
for ((i = 1; i <= $#tmpnames; i++)) ; do
if [ ! -f "$i" ] ; then
mv "$1" $tmpnames[i] && mv "$2" "$1" && mv $tmpnames[i] "$2"
return 0
fi
done
echo "tmpfile names are all in conflict!" >$2
return 1
}
mkold(){
if (( $# != 1 )) ; then
echo "Exact 1 command line arguments needed!" >&2
return 1
fi
local oldname="$1.old"
if [ -f "$oldname" ] ; then
echo "$oldname already exists!" >&2
return 1
fi
mv "$1" "$oldname"
}
rmold(){
if (( $# != 1 )) ; then
echo "Exact 1 command line arguments needed!" >&2
return 1
fi
if [ ${1:0-4} != ".old" ] ; then
echo "$1 is not an old!" >&2
return 1
fi
local name="${1:0:0-4}"
if [ -f "$name" ] ; then
echo "$name already exists!" >&2
return 1
fi
mv "$1" "$name"
}
shiftold(){
if (( $# != 2 )) ; then
echo "Exact 2 command line arguments needed!" >&2
return 1
fi
local oldname="$2.old"
if [ -f "$oldname" ] ; then
echo "$oldname already exists!" >&2
return 1
fi
mv "$2" "$oldname"
cp -r "$1" "$2"
}
cless(){
script -c "$*" /dev/null < /dev/null |& less
}
cpmd(){
if (( $# != 2 )) ; then
echo "Exact 2 command line arguments needed!" >&2
return 1
fi
mkdir -p `dirname "$2"` || true
cp -riv "$1" "$2"
}
tran(){
if (( $# <= 0 )) ; then
echo "Need argument(s)!" >&2
return 1
fi
trans -b :zh-CN "$*"
}
dict(){
if (( $# <= 0 )) ; then
echo "Need argument(s)!" >&2
return 1
fi
trans -d :zh-CN "$*"
}
gitup(){
upstream=master
if [[ $# > 0 ]] then
upstream=$1
fi
git log --reverse --pretty=%H origin/$upstream | grep -A 1 $(git rev-parse HEAD) | tail -n1 | xargs git reset --hard
}
rr(){
if [[ $# == 0 ]];
then
echo 'Argument needed!'
return 1
fi
while [[ -n $1 ]];
do
if [[ ! -e $1 ]];
then
echo "\"${1}\" doesn't exist, skipping."
else
echo -n "Are you SURE to remove \"${1}\" FOREVER ??? "
read ans
ans=`echo $ans | tr '[:upper:]' '[:lower:]'`
if [[ $ans == y || $and == yes ]];
then
\rm -rv "$1"
else
echo "Not removing \"${1}\""
fi
fi
shift
done
}
rf(){
if [[ $# == 0 ]];
then
echo 'Argument needed!'
return 1
fi
while [[ -n $1 ]];
do
if [[ ! -e $1 ]];
then
echo "\"${1}\" doesn't exist, skipping."
else
echo -n "Are you SURE to remove \"${1}\" FORCE and FOREVER ??? "
read ans
ans=`echo $ans | tr '[:upper:]' '[:lower:]'`
if [[ $ans == y || $and == yes ]];
then
\rm -rfv "$1"
else
echo "Not removing \"${1}\""
fi
fi
shift
done
}