-
Notifications
You must be signed in to change notification settings - Fork 0
/
zshrc.extras
190 lines (164 loc) · 4.58 KB
/
zshrc.extras
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
# utility functions
# this function checks if a command exists and returns either true
# or false. This avoids using 'which' and 'whence', which will
# avoid problems with aliases for which on certain weird systems. :-)
# Usage: check_com [-c|-g] word
# -c only checks for external commands
# -g does the usual tests and also checks for global aliases
check_com() {
emulate -L zsh
local -i comonly gatoo
if [[ $1 == '-c' ]] ; then
(( comonly = 1 ))
shift
elif [[ $1 == '-g' ]] ; then
(( gatoo = 1 ))
else
(( comonly = 0 ))
(( gatoo = 0 ))
fi
if (( ${#argv} != 1 )) ; then
printf 'usage: check_com [-c] <command>\n' >&2
return 1
fi
if (( comonly > 0 )) ; then
[[ -n ${commands[$1]} ]] && return 0
return 1
fi
if [[ -n ${commands[$1]} ]] \
|| [[ -n ${functions[$1]} ]] \
|| [[ -n ${aliases[$1]} ]] \
|| [[ -n ${reswords[(r)$1]} ]] ; then
return 0
fi
if (( gatoo > 0 )) && [[ -n ${galiases[$1]} ]] ; then
return 0
fi
return 1
}
# Create small urls via http://goo.gl using curl(1).
# API reference: https://code.google.com/apis/urlshortener/
function zurl() {
emulate -L zsh
if [[ -z $1 ]]; then
print "USAGE: zurl <URL>"
return 1
fi
local PN url prog api json data
PN=$0
url=$1
# Prepend 'http://' to given URL where necessary for later output.
if [[ ${url} != http(s|)://* ]]; then
url='http://'${url}
fi
if check_com -c curl; then
prog=curl
else
print "curl is not available, but mandatory for ${PN}. Aborting."
return 1
fi
api='https://www.googleapis.com/urlshortener/v1/url'
contenttype="Content-Type: application/json"
json="{\"longUrl\": \"${url}\"}"
data=$($prog --silent -H ${contenttype} -d ${json} $api)
# Match against a regex and print it
if [[ $data =~ '"id": "(http://goo.gl/[[:alnum:]]+)"' ]]; then
print $match;
fi
}
sub() {
if [[ -z $2 ]] then
print "Usage: sub from_string to_string"
exit 1
fi
grep -rl "$1" * | xargs replace "$1" "$2" --
}
#f5# Backup \kbd{file {\rm to} file\_timestamp}
bk() {
emulate -L zsh
cp -b $1 $1_`date --iso-8601=m`
}
#f5# cd to directoy and list files
cl() {
emulate -L zsh
cd $1 && ls -a
}
#f5# Create temporary directory and \kbd{cd} to it
cdt() {
local t
t=$(mktemp -d)
echo "$t"
builtin cd "$t"
}
# grep for running process, like: 'any vim'
any() {
emulate -L zsh
unsetopt KSH_ARRAYS
if [[ -z "$1" ]] ; then
echo "any - grep for process(es) by keyword" >&2
echo "Usage: any <keyword>" >&2 ; return 1
else
ps xauwww | grep -i "${grep_options[@]}" "[${1[1]}]${1[2,-1]}"
fi
}
100m() {
local url=$1
echo $url | grep ^https >/dev/null && local args=--no-check-certificate
wget $args -O /dev/null $url/100mb.bin
}
# listing stuff
alias ls="ls --color=auto"
#a2# Execute \kbd{ls -lSrah}
alias dir="ls -lSrah"
#a2# Only show dot-directories
alias lad='ls -d .*(/)'
#a2# Only show dot-files
alias lsa='ls -a .*(.)'
#a2# Only files with setgid/setuid/sticky flag
alias lss='ls -l *(s,S,t)'
#a2# Only show symlinks
alias lsl='ls -l *(@)'
#a2# Display only executables
alias lsx='ls -l *(*)'
#a2# Display world-{readable,writable,executable} files
alias lsw='ls -ld *(R,W,X.^ND/)'
#a2# Display the ten biggest files
alias lsbig="ls -flh *(.OL[1,10])"
#a2# Only show directories
alias lsd='ls -d *(/)'
#a2# Only show empty directories
alias lse='ls -d *(/^F)'
#a2# Display the ten newest files
alias lsnew="ls -rtlh *(D.om[1,10])"
#a2# Display the ten oldest files
alias lsold="ls -rtlh *(D.Om[1,10])"
#a2# Display the ten smallest files
alias lssmall="ls -Srl *(.oL[1,10])"
#a2# Display the ten newest directories and ten newest .directories
alias lsnewdir="ls -rthdl *(/om[1,10]) .*(D/om[1,10])"
#a2# Display the ten oldest directories and ten oldest .directories
alias lsolddir="ls -rthdl *(/Om[1,10]) .*(D/Om[1,10])"
#a3# remove vim undo history files
alias unclean="rm -f .*.un~"
# svn aliases
alias sd="svn diff"
alias sc="svn commit"
alias scm="svn commit -m"
alias sup="svn update"
alias sco="svn checkout"
insert-first-word() {
zle insert-last-word -- -1 1
}
insert-first-arg() {
zle insert-last-word -- -1 2
}
insert-second-arg() {
zle insert-last-word -- -1 3
}
zle -N insert-first-word
zle -N insert-first-arg
zle -N insert-second-arg
bindkey '^[0' insert-first-word
bindkey '^[-' insert-first-arg
bindkey '^[=' insert-second-arg
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh