zsh/aliases/functions

346 lines
7.5 KiB
Plaintext
Raw Normal View History

#!/bin/zsh
#################################################################################
# daily use aliases
##############################################################################{{{
2016-07-22 14:01:35 +00:00
alias ls="ls -N --color=auto"
alias ll="ls -hl --group-directories-first"
alias lll="ls -hla"
alias ds="du -sh"
alias :q="exit"
2018-05-14 13:07:53 +00:00
alias rget="rsync -rP --append-verify --inplace"
alias 7zu="7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on"
2017-10-08 22:58:05 +00:00
if command -v fd >/dev/null; then
alias f="fd"
else
f() { noglob find . -iname "*${*}*" }
fi
2016-11-23 08:58:17 +00:00
2014-06-27 05:42:56 +00:00
alias sudo="sudo "
alias S='sudo $(history -n -1)'
alias rm="rm -I"
2014-06-27 05:42:56 +00:00
alias v="xdg-open"
2018-09-11 17:26:04 +00:00
alias vmemcache="vmtouch -l -m 12G -v"
fork() {
( $* &>/dev/null & )
}
2017-11-12 22:20:53 +00:00
alias en="dict.cc.py de en"
alias de="dict.cc.py en de"
#}}}
#################################################################################
# zsh stuff
##############################################################################{{{
alias histoff="HISTFILE=/dev/null"
alias histon="HISTFILE=$HOME/.histfile"
2019-02-01 10:08:23 +00:00
E() {
if [[ -n $(jobs) ]]; then
echo "exec: You have suspended jobs."
else
builtin exec zsh
fi
}
# edit and refresh aliases
aliases() {
$EDITOR $(zdotfile aliases/${1:-functions})
. $(zdotfile aliases)/*
}
# per directory configuration
cd(){
if [ -e ".zout" ]; then
2013-05-16 11:14:41 +00:00
. "./.zout"
fi
builtin cd $*
if [ -e ".zin" ]; then
2013-05-16 11:14:41 +00:00
. "./.zin"
fi
}
#}}}
#################################################################################
# filesystem stuff
##############################################################################{{{
2016-07-22 14:01:35 +00:00
t=${TAGSISTANT_ROOT:-$HOME/tagged}/store
tagmount() {
tagsistant ${TAGSISTANT_ROOT:-$HOME/tagged} &>/tmp/tagsistant.$USER
}
tag() {
if [[ -z $2 ]]; then
printf "%s\n" \
"Usage: tag FILE... [-n name] [-v] [-p] TAG..." \
"" \
"Each TAG must end with the @ delimiter component." \
"Each File must have a name different from a single @"
return 1
fi
local name preview
zparseopts -D -E n:=name -name:=name p=preview -preview=preview \
v=verbose -verbose=verbose
local sources=()
local tags=()
for i in "${@}"; do
if [[ ${i:t} == "@" ]]; then
tags+=${i}
else
sources+=${i:A}
fi
done
for source in "${sources[@]}"; do
for tag in "${tags[@]}"; do
target=~t/$tag
if [[ ! -d $target ]]; then mkdir -p $target; fi
if [[ -n $verbose ]]; then echo "Tagging $source with $tag"; fi
${=preview:+echo} ln -s $source $target/${name[2]}
done
done
}
papertag() {
if [[ -z $2 ]]; then
echo "Usage: papertag FILE [-v] [-y YEAR] [AUTHOR...] [:: OTHER TAGS]"
return 1
fi
local year
zparseopts -D -E y:=year -year:=year \
v=verbose -verbose=verbose
local paper=$1; shift
tag $verbose $paper paper/@
if [[ -n "$year" ]]; then
tag $verbose $paper meta:/year/eq/${year[2]}/@/
fi
while [[ -n $1 ]]; do
local author=$1; shift
if [[ "$author" == "::" ]]; then break; fi
tag $verbose $paper meta:/author/eq/$author/@
done
for tag in "$@"; do
tag $verbose $paper $tag
done
}
tcd() {
rpath=$(realpath ~t/$1)
if [[ -d $rpath ]]; then
cd $rpath;
else
cd ${rpath:h}
fi
}
# count files in current or given directory
count() {
2014-06-27 05:42:56 +00:00
find $1 \( ! -path '*/.*' \) | wc -l
}
pumount() {
params=()
while [[ "${1[1]}" == "-" ]]; do
params+="$1"
shift
done
if [[ "${1[1]}" == "/" ]]; then
2014-06-27 05:42:56 +00:00
command pumount "${params[@]}" "$1"
else
2014-06-27 05:42:56 +00:00
command pumount "${params[@]}" "/media/$1"
fi
}
#}}}
#################################################################################
# filename manipulation
##############################################################################{{{
alias cleanspaces="renamexm -s/\ /_/g -R"
# prefix all given files with first argument to this function
prefix() {
pre=$1;shift
for i in $*; do mv $i $pre$i;done
}
# prefix all given files with consecutive numbers, starting with 1
# or with number given by -s switch (must be first argument)
prenumbering() {
if [[ "$1" == "-s" ]]; then
n=${2};
shift 2
else
n=1
fi
for i in "$@"; do
mv $i ${(l:2::0:)n}_$i;
((n++));
done
}
# appends given suffix (first argument)
suffix() {
suf=$1;shift
for i in $*; do mv $i $i$suf;done
}
#}}}
#################################################################################
# audio video photo
##############################################################################{{{
alias exifcopy="exiftool -tagsFromFile"
2016-01-05 02:43:27 +00:00
photobydate() {
2016-07-22 14:01:35 +00:00
for i in ${*:-(#i)*.(JPG|CR2)}; do
if [[ -e $i ]]; then
2016-01-05 02:43:27 +00:00
dir=$(exiftool -p '$DateTimeOriginal' $i | tr ':' '-' | cut -d' ' -f1;)
mkdir -p $dir
mv ${i:r}.* $dir
fi
2016-01-05 02:43:27 +00:00
done
}
alias mkvidentify="mkvmerge --identify"
2016-01-05 02:43:27 +00:00
cropdetect() {
ffmpeg -ss ${2:-00:05:00} -t 1 -i "${1}" -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1
}
#}}}
#################################################################################
# programming
##############################################################################{{{
# watch latex file for changes and rebuild
latexwatch() {
maindoc=$1
while : ; do
inotifywait -e modify $*; pdflatex -halt-on-error $maindoc
done
}
2016-01-05 02:43:27 +00:00
svn() {
case $1 in
log|diff)
command svn "$@" | $PAGER ;;
*)
command svn "$@" ;;
esac
}
2016-07-22 14:01:35 +00:00
alias gitv='vim "$(git rev-parse --git-dir)/index" -c "Gitv --all" -c tabonly'
#}}}
#################################################################################
# noglobs
##############################################################################{{{
alias ri=noglob\ ri
alias wcalc="noglob wcalc"
#}}}
#################################################################################
# tmux
##############################################################################{{{
tdetachprep() {
env -i tmux new-session -d -s detached &>/dev/null
}
alias :u="tmux select-pane -U"
alias :d="tmux select-pane -D"
alias :l="tmux select-pane -L"
alias :r="tmux select-pane -R"
alias :split="tmux splitw -v"
alias :vsplit="tmux splitw -h"
alias :detachw="tdetachprep; tmux movew -t detached: -s"
alias :attachw="tdetachprep; tmux movew -t : -s"
#}}}
2016-01-05 02:43:27 +00:00
#################################################################################
# btrfs
##############################################################################{{{
btrfs-subvolume-force-delete() {
btrfs property set "$1" ro false
btrfs subvolume delete "$1"
}
#}}}
#
#################################################################################
# global and suffix aliases
##############################################################################{{{
# lazy shortcuts
alias -g G="| grep"
alias -g L="| less"
alias -g T="| tail"
# xclipboard
alias -g XS='$(xclip -o -selection primary)'
alias -g XC='$(xclip -o -selection clipboard)'
alias -s log=vimpager
#}}}
2014-06-27 05:42:56 +00:00
#################################################################################
# various small scripts
##############################################################################{{{
urlencode() {
local input
setopt extendedglob
if [ -n "$1" ]; then
input="$*"
else
input=$(</dev/stdin)
fi
# by jkramer, source: http://stackoverflow.com/a/187853/928769
echo "${${(j: :)input}//(#b)(?)/%$[[##16]##${match[1]}]}"
}
#}}}
2017-03-06 16:14:54 +00:00
eth-ip() {
ip a show primary dev ${1:-/sys/class/net/en*~*/lo(:t)} | awk '/\s*inet / { split($2,a,"/");print a[1] }'
}
2014-06-27 05:42:56 +00:00
fasel-tunnel() {
if [[ -z "$1" || -z "$2" || "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: fasel-tunnel <host> <port> [remote_port]"
return
fi
local host="$1";
local port="$2";
local remote_port="${3:-$2}"
ssh -L $port:$host:$remote_port fasel -N &
}
2017-11-12 22:20:53 +00:00
tmpssh() {
dir=/tmp/${${1#*:}:t}
mkdir -p ${dir}
sshfs $1 ${dir}
cd ${dir}
}
# vim: foldmethod=marker