Skip to content

Instantly share code, notes, and snippets.

@DHowett
Forked from rpetrich/.bash_profile
Created February 20, 2011 00:05
Show Gist options
  • Save DHowett/835531 to your computer and use it in GitHub Desktop.
Save DHowett/835531 to your computer and use it in GitHub Desktop.

Revisions

  1. DHowett revised this gist Feb 20, 2011. 1 changed file with 69 additions and 22 deletions.
    91 changes: 69 additions & 22 deletions .zshrc
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,81 @@
    # shell bookmarks; s <name> to save, l to list and g <name> to go
    # optimize some cases, include zsh completion, and change the bookmark storage format (_BOOKMARK_ != DIR_ to avoid namespace pollution)
    function _uh { [[ -z $DEV ]]; return $? }

    function userroot {
    if ! _uh; then echo $DEV;
    else echo $HOME; fi
    }

    function _sdirname {
    echo "$(userroot)/.sdirs"
    }

    function _sglobalname {
    echo "${HOME}/.sdirs"
    }

    function _ss_for_arg {
    local d n;
    if [[ -z $1 ]]; then return; fi
    if [[ "${1[0,1]}" == "+" ]]; then d="$(_sglobalname)"; n="${1[2,-1]}";
    else d="$(_sdirname)"; n="$1"; fi
    echo "local sdir arg; sdir=\"$d\"; arg=\"$n\";"
    }

    # Shell Bookmarks (inspired by rpetrich.)
    function dbg {
    if [[ -z $1 ]]; then return; fi
    eval $(_ss_for_arg $1);
    echo $sdir;
    echo $arg;
    }
    function s {
    if [[ -z $1 ]]; then return; fi
    if [[ -f ~/.sdirs ]]; then
    grep -v "^export _BOOKMARK_$1" ~/.sdirs > ~/.sdirs1
    mv ~/.sdirs1 ~/.sdirs
    fi
    echo "export _BOOKMARK_$1=$PWD" >> ~/.sdirs
    if [[ -z $1 ]]; then return; fi
    eval $(_ss_for_arg $1);
    if [[ -f "$sdir" ]]; then
    grep -v "^export _BOOKMARK_$arg" "${sdir}" > "${sdir}1"
    mv "${sdir}1" "${sdir}"
    fi
    echo "export _BOOKMARK_$arg=$PWD" >> "${sdir}"
    }

    function l {
    if [[ -s ~/.sdirs ]]; then
    sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/\1 (\2)/g' ~/.sdirs | sort
    else
    echo "No bookmarks."
    fi
    if [[ -s $(_sdirname) ]]; then
    sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/\1 (\2)/g' "$(_sdirname)" | sort
    fi
    if ! _uh; then
    if [[ -s $(_sglobalname) ]]; then
    sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/+\1 (\2)/g' "$(_sglobalname)" | sort
    fi
    fi
    }

    function g {
    . ~/.sdirs
    cd $(eval echo $(echo \$_BOOKMARK_$1))
    eval $(_ss_for_arg $1)
    . "$sdir"
    cd $(eval echo $(echo \$_BOOKMARK_$arg))
    }

    function d {
    if [[ -z $1 ]]; then return; fi
    if [[ -f ~/.sdirs ]]; then
    grep -v "^export _BOOKMARK_$1" ~/.sdirs > ~/.sdirs1
    mv ~/.sdirs1 ~/.sdirs
    fi
    if [[ -z $1 ]]; then return; fi
    eval $(_ss_for_arg $1)
    if [[ -f "$sdir" ]]; then
    grep -v "^export _BOOKMARK_$arg" "${sdir}" > "${sdir}1"
    mv "${sdir}1" "${sdir}"
    fi
    }

    function _gcomp { reply=(`sed -e 's/^export _BOOKMARK_\([^=]*\)=.*$/\1/g' ~/.sdirs | sort`) }
    compctl -K _gcomp g
    function _gcomp {
    local descs globals;
    descs=();
    globals=();
    sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/\1:\2/g' "$(_sdirname)" | sort | while read line; do
    descs[$(($#descs+1))]="$line"
    done
    if ! _uh; then
    sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/+\1:\2/g' "$(_sglobalname)" | sort | while read line; do
    globals[$(($#globals+1))]="$line"
    done
    fi
    _describe -t bookmarks "shell bookmark" descs; _describe -t bookmarks "global bookmark" globals
    }
    compdef _gcomp g
  2. DHowett revised this gist Feb 20, 2011. 2 changed files with 34 additions and 28 deletions.
    28 changes: 0 additions & 28 deletions .bash_profile
    Original file line number Diff line number Diff line change
    @@ -1,28 +0,0 @@
    # shell bookmarks; s <name> to save, l to list and g <name> to go
    function s {
    cat ~/.sdirs | grep -v "export DIR_$1=" > ~/.sdirs1
    mv ~/.sdirs1 ~/.sdirs
    echo "export DIR_$1=$PWD" >> ~/.sdirs
    }
    function l {
    source ~/.sdirs
    env | grep "^DIR_" | cut -c5- | grep "^.*=" | sort
    }
    function g {
    source ~/.sdirs
    cd $(eval $(echo echo $(echo \$DIR_$1)))
    }
    # enable custom tab completion on g
    shopt -s progcomp
    function _l {
    source ~/.sdirs
    env | grep "^DIR_" | cut -c5- | grep "^.*=" | cut -f1 -d "="
    }
    function _gcomp {
    local curw
    COMPREPLY=()
    curw=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=($(compgen -W '`_l`' -- $curw))
    return 0
    }
    complete -F _gcomp g
    34 changes: 34 additions & 0 deletions .zshrc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # shell bookmarks; s <name> to save, l to list and g <name> to go
    # optimize some cases, include zsh completion, and change the bookmark storage format (_BOOKMARK_ != DIR_ to avoid namespace pollution)
    function s {
    if [[ -z $1 ]]; then return; fi
    if [[ -f ~/.sdirs ]]; then
    grep -v "^export _BOOKMARK_$1" ~/.sdirs > ~/.sdirs1
    mv ~/.sdirs1 ~/.sdirs
    fi
    echo "export _BOOKMARK_$1=$PWD" >> ~/.sdirs
    }

    function l {
    if [[ -s ~/.sdirs ]]; then
    sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/\1 (\2)/g' ~/.sdirs | sort
    else
    echo "No bookmarks."
    fi
    }

    function g {
    . ~/.sdirs
    cd $(eval echo $(echo \$_BOOKMARK_$1))
    }

    function d {
    if [[ -z $1 ]]; then return; fi
    if [[ -f ~/.sdirs ]]; then
    grep -v "^export _BOOKMARK_$1" ~/.sdirs > ~/.sdirs1
    mv ~/.sdirs1 ~/.sdirs
    fi
    }

    function _gcomp { reply=(`sed -e 's/^export _BOOKMARK_\([^=]*\)=.*$/\1/g' ~/.sdirs | sort`) }
    compctl -K _gcomp g
  3. @rpetrich rpetrich created this gist Feb 19, 2011.
    28 changes: 28 additions & 0 deletions .bash_profile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    # shell bookmarks; s <name> to save, l to list and g <name> to go
    function s {
    cat ~/.sdirs | grep -v "export DIR_$1=" > ~/.sdirs1
    mv ~/.sdirs1 ~/.sdirs
    echo "export DIR_$1=$PWD" >> ~/.sdirs
    }
    function l {
    source ~/.sdirs
    env | grep "^DIR_" | cut -c5- | grep "^.*=" | sort
    }
    function g {
    source ~/.sdirs
    cd $(eval $(echo echo $(echo \$DIR_$1)))
    }
    # enable custom tab completion on g
    shopt -s progcomp
    function _l {
    source ~/.sdirs
    env | grep "^DIR_" | cut -c5- | grep "^.*=" | cut -f1 -d "="
    }
    function _gcomp {
    local curw
    COMPREPLY=()
    curw=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=($(compgen -W '`_l`' -- $curw))
    return 0
    }
    complete -F _gcomp g