zshrc/functions.zsh
2011-09-17 13:08:06 +02:00

167 lines
5 KiB
Bash

# @author Sebastian Tramp <mail@sebastian.tramp.name>
# @license http://opensource.org/licenses/gpl-license.php
#
# functions and key bindings to that functions
#
# strg+x,s adds sudo to the line
# Zsh Buch p.159 - http://zshbuch.org/
run-with-sudo() { LBUFFER="sudo $LBUFFER" }
zle -N run-with-sudo
bindkey '^Xs' run-with-sudo
# Top ten memory hogs
# http://www.commandlinefu.com/commands/view/7139/top-ten-memory-hogs
memtop() {ps -eorss,args | sort -nr | pr -TW$COLUMNS | head}
zle -N memtop
tmux-hglog() {
tmux kill-pane -t 1
tmux split-window -h -l 40 "while true; do clear; date; echo; hg xlog-small -l 5 || exit; sleep 600; done;"
tmux select-pane -t 0
}
# tmux-neww-in-cwd - open a new shell with same cwd as calling pane
# http://chneukirchen.org/dotfiles/bin/tmux-neww-in-cwd
tmux-neww-in-cwd() {
SIP=$(tmux display-message -p "#S:#I:#P")
PTY=$(tmux server-info |
egrep flags=\|bytes |
awk '/windows/ { s = $2 }
/references/ { i = $1 }
/bytes/ { print s i $1 $2 } ' |
grep "$SIP" |
cut -d: -f4)
PTS=${PTY#/dev/}
PID=$(ps -eao pid,tty,command --forest | awk '$2 == "'$PTS'" {print $1; exit}')
DIR=$(readlink /proc/$PID/cwd)
tmux neww "cd '$DIR'; $SHELL"
}
# Escape potential tarbombs
# http://www.commandlinefu.com/commands/view/6824/escape-potential-tarbombs
etb() {
l=$(tar tf $1);
if [ $(echo "$l" | wc -l) -eq $(echo "$l" | grep $(echo "$l" | head -n1) | wc -l) ];
then tar xf $1;
else mkdir ${1%.t(ar.gz||ar.bz2||gz||bz||ar)} && tar xvf $1 -C ${1%.t(ar.gz||ar.bz2||gz||bz||ar)};
fi ;
}
# show newest files
# http://www.commandlinefu.com/commands/view/9015/find-the-most-recently-changed-files-recursively
newest () {find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | grep -v cache | grep -v ".hg" | grep -v ".git" | sort -r | less }
# http://www.commandlinefu.com/commands/view/7294/backup-a-file-with-a-date-time-stamp
buf () {
oldname=$1;
if [ "$oldname" != "" ]; then
datepart=$(date +%Y-%m-%d);
firstpart=`echo $oldname | cut -d "." -f 1`;
newname=`echo $oldname | sed s/$firstpart/$firstpart.$datepart/`;
cp -R ${oldname} ${newname};
fi
}
atomtitles () { curl --silent $1 | xmlstarlet sel -N atom="http://www.w3.org/2005/Atom" -t -m /atom:feed/atom:entry -v atom:title -n}
function printHookFunctions () {
print -C 1 ":::pwd_functions:" $chpwd_functions
print -C 1 ":::periodic_functions:" $periodic_functions
print -C 1 ":::precmd_functions:" $precmd_functions
print -C 1 ":::preexec_functions:" $preexec_functions
print -C 1 ":::zshaddhistory_functions:" $zshaddhistory_functions
print -C 1 ":::zshexit_functions:" $zshexit_functions
}
# reloads all functions
# http://www.zsh.org/mla/users/2002/msg00232.html
r() {
local f
f=(~/.zsh/functions.d/*(.))
unfunction $f:t 2> /dev/null
autoload -U $f:t
}
# activates zmv
autoload zmv
# noglob so you don't need to quote the arguments of zmv
# mmv *.JPG *.jpg
alias mmv='noglob zmv -W'
# start a webcam for screencast
function webcam () {
mplayer -cache 128 -tv driver=v4l2:width=350:height=350 -vo xv tv:// -noborder -geometry "+1340+445" -ontop -quiet 2>/dev/null >/dev/null
}
# Rename files in a directory in an edited list fashion
# http://www.commandlinefu.com/commands/view/7818/
function massmove () {
ls > ls; paste ls ls > ren; vi ren; sed 's/^/mv /' ren|bash; rm ren ls
}
# Put a console clock in top right corner
# http://www.commandlinefu.com/commands/view/7916/
function clock () {
while sleep 1;
do
tput sc
tput cup 0 $(($(tput cols)-29))
date
tput rc
done &
}
function apt-import-key () {
gpg --keyserver subkeys.pgp.net --recv-keys $1 | gpg --armor --export $1 | sudo apt-key add -
}
# create a new script, automatically populating the shebang line, editing the
# script, and making it executable.
# http://www.commandlinefu.com/commands/view/8050/
shebang() {
if i=$(which $1);
then
printf '#!/usr/bin/env %s\n\n' $1 > $2 && chmod 755 $2 && vim + $2 && chmod 755 $2;
else
echo "'which' could not find $1, is it in your \$PATH?";
fi;
# in case the new script is in path, this throw out the command hash table and
# start over (man zshbuiltins)
rehash
}
# a rough equivalent to "hg out"
# http://www.doof.me.uk/2011/01/08/list-outgoing-changesets-in-git/
git-out() {
for i in $(git push -n $* 2>&1 | awk '$1 ~ /[a-f0-9]+\.\.[a-f0-9]+/ { print $1; }')
do
git xlog $i
done
}
# Query Wikipedia via console over DNS
# http://www.commandlinefu.com/commands/view/2829
wp() {
dig +short txt ${1}.wp.dg.cx
}
# translate via google language tools (more lightweight than leo)
# http://www.commandlinefu.com/commands/view/5034/
translate() {
wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'
}
# cd to the root of the current vcs repository
gr() {
# vcsroot=`echo $vcs_info_msg_0_ | cut -d "|" -f 5`
vcsroot=`/home/seebi/.vim/scripts/vcsroot.sh`
echo $vcsroot && cd $vcsroot
}