From 05db802e973abc999ec4ec4f4e41087c649df027 Mon Sep 17 00:00:00 2001 From: David Runge Date: Sun, 8 May 2016 02:59:18 +0200 Subject: .zsh.functions/*: Adding functions from several zprezto modules. --- .zsh.functions/02_utility.zsh | 104 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 .zsh.functions/02_utility.zsh (limited to '.zsh.functions/02_utility.zsh') diff --git a/.zsh.functions/02_utility.zsh b/.zsh.functions/02_utility.zsh new file mode 100644 index 0000000..c08ce10 --- /dev/null +++ b/.zsh.functions/02_utility.zsh @@ -0,0 +1,104 @@ +# Makes a directory and changes to it. +function mkdcd { + [[ -n "$1" ]] && mkdir -p "$1" && builtin cd "$1" +} + +# Changes to a directory and lists its contents. +function cdls { + builtin cd "$argv[-1]" && ls "${(@)argv[1,-2]}" +} + +# Pushes an entry onto the directory stack and lists its contents. +function pushdls { + builtin pushd "$argv[-1]" && ls "${(@)argv[1,-2]}" +} + +# Pops an entry off the directory stack and lists its contents. +function popdls { + builtin popd "$argv[-1]" && ls "${(@)argv[1,-2]}" +} + +# Prints columns 1 2 3 ... n. +function slit { + awk "{ print ${(j:,:):-\$${^@}} }" +} + +# Finds files and executes a command on them. +function find-exec { + find . -type f -iname "*${1:-}*" -exec "${2:-file}" '{}' \; +} + +# Displays user owned processes status. +function psu { + ps -U "${1:-$LOGNAME}" -o 'pid,%cpu,%mem,command' "${(@)argv[2,-1]}" +} + +# Highlights diff output. +function diff { + if (( $+commands[colordiff] )); then + command diff --unified "$@" | colordiff --difftype diffu + elif (( $+commands[git] )); then + git --no-pager diff --color=auto --no-ext-diff --no-index "$@" + else + command diff --unified "$@" + fi +} + +# Highlights make output. +function make { + if (( $+commands[colormake] )); then + colormake "$@" + else + command make "$@" + fi +} + +# Highlights wdiff output. +function wdiff { + if (( $+commands[wdiff] )); then + command wdiff \ + --avoid-wraps \ + --start-delete="$(print -n $FG[red])" \ + --end-delete="$(print -n $FG[none])" \ + --start-insert="$(print -n $FG[green])" \ + --end-insert="$(print -n $FG[none])" \ + "$@" \ + | sed 's/^\(@@\( [+-][[:digit:]]*,[[:digit:]]*\)\{2\} @@\)$/;5;6m\10m/g' + elif (( $+commands[git] )); then + git --no-pager diff --color=auto --no-ext-diff --no-index --color-words "$@" + else + command wdiff "$@" + fi +} + +# Lists the contents of archives. +function lsarchive { + while (( $# > 0 )); do + if [[ ! -s "$1" ]]; then + print "$0: file not valid: $1" >&2 + shift + continue + fi + case "$1:l" in + (*.tar.gz|*.tgz) tar t${verbose:+v}vzf "$1" ;; + (*.tar.bz2|*.tbz|*.tbz2) tar t${verbose:+v}jf "$1" ;; + (*.tar.xz|*.txz) tar --xz --help &> /dev/null \ + && tar --xz -t${verbose:+v}f "$1" \ + || xzcat "$1" | tar t${verbose:+v}f - ;; + (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \ + && tar --lzma -t${verbose:+v}f "$1" \ + || lzcat "$1" | tar x${verbose:+v}f - ;; + (*.tar) tar t${verbose:+v}f "$1" ;; + (*.zip) unzip -l${verbose:+v} "$1" ;; + (*.rar) unrar &> /dev/null \ + && unrar ${${verbose:+v}:-l} "$1" \ + || rar ${${verbose:+v}:-l} "$1" ;; + (*.7z) 7za l "$1" ;; + (*) + print "$0: cannot list: $1" >&2 + success=1 + ;; + esac + shift + done +} -- cgit v1.2.3-70-g09d2