3 #f5# Backup \kbd{file {\rm to} file\_timestamp}
6 cp -b $1 $1_`date --iso-8601=m`
9 #f5# cd to directoy and list files
15 #f5# Create Directoy and \kbd{cd} to it
17 mkdir
-p "$@" && cd "$@"
22 ps aux |
awk '{if (NR > 1) print $5; if (NR > 2) print "+"} END { print "p" }' |
dc
25 # Function Usage: doc packagename
26 #f5# \kbd{cd} to /usr/share/doc/\textit{package}
29 cd /usr
/share
/doc
/$1 && ls
31 _doc
() { _files
-W /usr
/share
/doc
-/ }
32 check_com compdef
&& compdef _doc doc
40 #f5# Watch manpages in a stretched style
41 man2
() { PAGER
='dash -c "sed G | /usr/bin/less"' command man
"$@" ; }
43 #f5# Clean up directory - remove well known tempfiles
45 FILES
=(*~
(N
) .
*~
(N
) \
#*\#(N) *.o(N) a.out(N) *.core(N) *.cmo(N) *.cmi(N) .*.swp(N) .*.COMPILED)
47 if [[ $NBFILES > 0 ]] ; then
50 echo -n "Remove these files? [y/n] "
52 if [[ $ans == "y" ]] ; then
54 echo ">> $PWD purged, $NBFILES files removed"
56 echo "Ok. .. then not.."
61 #f5# List all occurrences of programm in current PATH
64 if [[ $# = 0 ]] ; then
65 echo "Usage: $0 program"
66 echo "Example: $0 zsh"
67 echo "Lists all occurrences of program in the current PATH."
69 ls -l ${^path}/*$1*(*N
)
73 # Usage: simple-extract <file>
74 #f5# Smart archive extractor
79 *.
tar.bz2
) bzip2 -v -d $1 ;;
80 *.
tar.gz
) tar -xvzf $1 ;;
87 *.tgz
) gunzip
-d $1 ;;
88 *.tbz2
) tar -jxvf $1 ;;
91 *) echo "'$1' Error. Please go away" ;;
94 echo "'$1' is not a valid file"
98 # Usage: smartcompress <file> (<type>)
99 #f5# Smart archive creator
102 if [[ -n $2 ]] ; then
104 tgz |
tar.gz
) tar -zcvf$1.
$2 $1 ;;
105 tbz2 |
tar.bz2
) tar -jcvf$1.
$2 $1 ;;
106 tar.Z
) tar -Zcvf$1.
$2 $1 ;;
107 tar) tar -cvf$1.
$2 $1 ;;
108 gz |
gzip) gzip $1 ;;
109 bz2 |
bzip2) bzip2 $1 ;;
111 echo "Error: $2 is not a valid compression type"
115 smartcompress
$1 tar.gz
119 # Usage: show-archive <archive>
120 #f5# List an archive's content
123 if [[ -f $1 ]] ; then
125 *.
tar.gz
) gunzip
-c $1 |
tar -tf - -- ;;
127 *.tgz
) tar -ztf $1 ;;
128 *.
zip) unzip -l $1 ;;
130 *.deb
) dpkg-deb
--fsys-tarfile $1 |
tar -tf - -- ;;
131 *) echo "'$1' Error. Please go away" ;;
134 echo "'$1' is not a valid archive"
138 # It's shameless stolen from <http://www.vim.org/tips/tip.php?tip_id=167>
139 #f5# Use \kbd{vim} as your manpage reader
142 man $
* |
col -b | view
-c 'set ft=man nomod nolist' -
145 # TODO: So, this is the third incarnation of this function!?
146 #f5# Reload given functions
148 for func
in $argv ; do
154 # a small check to see which DIR is located on which server/partition.
155 # stolen and modified from Sven's zshrc.forall
156 #f5# Report diskusage of a directory
159 if [[ -n "$1" ]] ; then
161 if [[ -d "$dir" ]] ; then
162 ( cd $dir; echo "-<$dir>"; du
-shx .
; echo);
164 echo "warning: $dir does not exist" >&2
169 if [[ -d "$dir" ]] ; then
170 ( cd $dir; echo "-<$dir>"; du
-shx .
; echo);
172 echo "warning: $dir does not exist" >&2
178 #f5# Show some status info
181 print
"Date..: "$
(date "+%Y-%m-%d %H:%M:%S")
182 print
"Shell.: Zsh $ZSH_VERSION (PID = $$, $SHLVL nests)"
183 print
"Term..: $TTY ($TERM), ${BAUD:+$BAUD bauds, }$COLUMNS x $LINES chars"
184 print
"Login.: $LOGNAME (UID = $EUID) on $HOST"
185 print
"Groups: $(groups)"
186 print
"System: $(cat /etc/[A-Za-z]*[_-][rv]e[lr]*)"
187 print
"Uptime:$(uptime)"
191 #f5# RFC 2396 URL encoding in Z-Shell
196 print
${(j::)input/(#b)([^A-Za-z0-9_.!~*\'\(\)-])/%${(l:2::0:)$(([##16]#match))}}
199 #f5# Indent source code
201 indent
-npro -kr -i8 -ts8 -sob -l80 -ss -ncs "$@"
204 # hl() highlighted less
205 # http://ft.bewatermyfriend.org/comp/data/zsh/zfunct.html
206 if check_com
-c highlight
; then
210 theme
=${HL_THEME:-""}
213 ( printf 'available languages (syntax parameter):\n\n' ;
214 highlight
--list-langs ; ) |
less -SMr
217 ( printf 'available themes (style parameter):\n\n' ;
218 highlight
--list-themes ; ) |
less -SMr
221 printf 'usage: hl <syntax[:theme]> <file>\n'
222 printf ' available options: --list (-l), --themes (-t), --help (-h)\n\n'
223 printf ' Example: hl c main.c\n'
226 if [[ -z ${2} ]] ||
(( ${#argv} > 2 )) ; then
227 printf 'usage: hl <syntax[:theme]> <file>\n'
228 printf ' available options: --list (-l), --themes (-t), --help (-h)\n'
229 (( ${#argv} > 2 )) && printf ' Too many arguments.\n'
233 [[ ${1} == *:* ]] && [[ -n ${1#*:} ]] && theme=${1#*:}
234 if [[ -n ${theme} ]] ; then
235 highlight
--xterm256 --syntax ${lang} --style ${theme} ${2} |
less -SMr
237 highlight
--ansi --syntax ${lang} ${2} |
less -SMr
243 # ... and a proper completion for hl()
244 # needs 'highlight' as well, so it fits fine in here.
245 function _hl_genarg
() {
247 if [[ -prefix 1 *: ]] ; then
249 themes
=(${${${(f)"$(LC_ALL=C highlight --list-themes)"}/ #/}:#*(Installed|Use name)*})
251 _wanted
-C list themes expl theme compadd
${themes}
254 langs
=(${${${(f)"$(LC_ALL=C highlight --list-langs)"}/ #/}:#*(Installed|Use name)*})
255 _wanted
-C list languages expl languages compadd
-S ':' -q ${langs}
258 function _hl_complete
() {
259 _arguments
-s '1: :_hl_genarg' '2:files:_path_files'
261 compdef _hl_complete hl
264 # Create small urls via http://tinyurl.com using wget(1).
267 [[ -z $1 ]] && { print
"USAGE: tinyurl <URL>" ; return 1 }
269 local PN url tiny grabber search result preview
272 # Check existence of given URL with the help of ping(1).
273 # N.B. ping(1) only works without an eventual given protocol.
274 ping -c 1 ${${url#(ftp|http)://}%%/*} >& /dev
/null || \
275 read -q "?Given host ${${url#http://*/}%/*} is not reachable by pinging. Proceed anyway? [y|n] "
277 if (( $?
== 0 )) ; then
278 # Prepend 'http://' to given URL where necessary for later output.
279 [[ ${url} != http
(s|
)://* ]] && url
='http://'${url}
280 tiny
='http://tinyurl.com/create.php?url='
281 if check_com
-c wget
; then
282 grabber
='wget -O- -o/dev/null'
284 print
"wget is not available, but mandatory for ${PN}. Aborting."
286 # Looking for i.e.`copy('http://tinyurl.com/7efkze')' in TinyURL's HTML code.
287 search
='copy\(?http://tinyurl.com/[[:alnum:]]##*'
288 result
=${(M)${${${(f)"$(${=grabber} ${tiny}${url})"}[(fr)${search}*]}//[()\';]/}%%http
:*}
289 # TinyURL provides the rather new feature preview for more confidence. <http://tinyurl.com/preview.php>
290 preview
='http://preview.'${result#http://}
292 printf '%s\n\n' "${PN} - Shrinking long URLs via webservice TinyURL <http://tinyurl.com>."
293 printf '%s\t%s\n\n' 'Given URL:' ${url}
294 printf '%s\t%s\n\t\t%s\n' 'TinyURL:' ${result} ${preview}
300 #f2# Print a specific line of file(s).
304 if [ $# -lt 2 ] ; then
305 print
"Usage: linenr <number>[,<number>] <file>" ; return 1
306 elif [ $# -eq 2 ] ; then
309 command ed
-s $file <<< "${number}n"
313 for file in "$@" ; do
314 if [ ! -d $file ] ; then
316 command ed
-s $file <<< "${number}n" 2> /dev
/null
325 # use it e.g. via 'Restart apache2'
326 #m# f6 Start() \kbd{/etc/init.d/\em{process}}\quad\kbd{start}
327 #m# f6 Restart() \kbd{/etc/init.d/\em{process}}\quad\kbd{restart}
328 #m# f6 Stop() \kbd{/etc/init.d/\em{process}}\quad\kbd{stop}
329 #m# f6 Reload() \kbd{/etc/init.d/\em{process}}\quad\kbd{reload}
330 #m# f6 Force-Reload() \kbd{/etc/init.d/\em{process}}\quad\kbd{force-reload}
331 if [[ -d /etc
/init.d ||
-d /etc
/service
]] ; then
333 local action_
="${1:l}" # e.g Start/Stop/Restart
337 local service_target_
="$(readlink /etc/init.d/$service_)"
338 if [[ $service_target_ == "/usr/bin/sv" ]]; then
341 start
) if [[ ! -e /etc
/service
/$service_ ]]; then
342 $SUDO ln -s "/etc/sv/$service_" "/etc/service/"
344 $SUDO "/etc/init.d/$service_" "${action_}" "$param_"
346 # there is no reload in runits sysv emulation
347 reload
) $SUDO "/etc/init.d/$service_" "force-reload" "$param_" ;;
348 *) $SUDO "/etc/init.d/$service_" "${action_}" "$param_" ;;
352 $SUDO "/etc/init.d/$service_" "${action_}" "$param_"
356 for i
in Start Restart Stop Force-Reload Reload
; do
357 eval "$i() { __start_stop $i \"\$1\" \"\$2\" ; }"
361 # grep for running process, like: 'any vim'
364 if [[ -z "$1" ]] ; then
365 echo "any - grep for process(es) by keyword" >&2
366 echo "Usage: any <keyword>" >&2 ; return 1
369 local LENGTH
=$
(expr length
$STRING)
370 local FIRSCHAR
=$
(echo $
(expr substr
$STRING 1 1))
371 local REST
=$
(echo $
(expr substr
$STRING 2 $LENGTH))
372 ps xauwww|
grep "[$FIRSCHAR]$REST"
376 # After resuming from suspend, system is paging heavily, leading to very bad interactivity.
377 # taken from $LINUX-KERNELSOURCE/Documentation/power/swsusp.txt
378 [[ -r /proc
/1/maps
]] && \
380 print
'Reading /proc/[0-9]*/maps and sending output to /dev/null, this might take a while.'
381 cat $
(sed -ne 's:.* /:/:p' /proc
/[0-9]*/maps |
sort -u |
grep -v '^/dev/') > /dev
/null
382 print
'Finished, running "swapoff -a; swapon -a" may also be useful.'
385 # print hex value of a number
388 [[ -n "$1" ]] && printf "%x\n" $1 ||
{ print
'Usage: hex <number-to-convert>' ; return 1 }
391 # calculate (or eval at all ;-)) with perl => p[erl-]eval
392 # hint: also take a look at zcalc -> 'autoload zcalc' -> 'man zshmodules | less -p MATHFUNC'
394 [[ -n "$1" ]] && CALC
="$*" || print
"Usage: calc [expression]"
395 perl
-e "print eval($CALC),\"\n\";"
397 functions peval
&>/dev
/null
&& alias calc
=peval
399 # brltty seems to have problems with utf8 environment and/or font Uni3-Terminus16 under
400 # certain circumstances, so work around it, no matter which environment we have
402 if [[ -z "$DISPLAY" ]] ; then
403 consolechars
-f /usr
/share
/consolefonts
/default8x16.psf.gz
413 cp $1 /tmp
/$F1 && cp $2 $1 && mv /tmp
/$F1 $2