zshrc.d/30-functions.zrc
author Mikael Berthe <mikael@lilotux.net>
Sat, 14 Jul 2012 23:13:35 +0200
changeset 6 7671e95ab8b0
parent 5 5dbc9ebf690c
child 37 9c238177f98a
permissions -rw-r--r--
Change my local mkcd alias into a function I was grepping for my mkcd alias in the wrong terminal and found another version, which turned out to be a good thing as Frank's implementation looks better :) https://dev.0x50.de/projects/ftzsh/repository/revisions/master/entry/functions/mkcd


#############
# Functions #
#############

# Commande tres pratique pour formatter un fichier .man
# exemple : mf prog.1
function mf() { tbl $* | nroff -mandoc | less -s }

# ,,(), ,,,() for quickly changing $CWD {{{1
# http://www.shell-fu.org/lister.php?id=769

# Go up n levels:
# ,, 3
function ,, (){
    local arg=${1:-1};
    local dir=""
    while [ $arg -gt 0 ]; do
        dir="../$dir"
        arg=$(($arg - 1));
    done
    cd $dir >&/dev/null
}

# Go up to a named dir
# ,,, usr
function ,,, (){
    if [ -z "$1" ]; then
        return
    fi
    local maxlvl=16
    local dir=$1
    while [ $maxlvl -gt 0 ]; do
        dir="../$dir"
        maxlvl=$(($maxlvl - 1));
        if [ -d "$dir" ]; then
            cd $dir >&/dev/null
        fi
    done
}

# This function seen in Frank Terbeck config files is an improvement over
# my previous mkcd alias...
functions mkcd() {
    [[ -z $1 ]] && printf 'usage: mkcd NEW-DIRECTORY\n' && return 1
    [[ -d $1 ]] && printf 'mkcd: Directory %s already exists; cd-ing\n' $1
    command mkdir -p -- $1
    builtin cd -- $1
}

# genpass()
# Generates a tough password of a given length
# Borrowed (and slightly modifed) from Seth House github repository.

function genpass() {
    if [ ! "$1" ]; then
        echo "Usage: $0 20"
        echo "For a random, 20-character password."
        return 1
    fi
    dd if=/dev/urandom count=1 2>/dev/null | tr -cd 'A-Za-z0-9' | cut -c-$1
}