Posts Tagged ‘software

20
Aug
13

Improved bash directory bookmarking

I’d recently found http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html and decided to give it a try.

It’s simple and quite stable. AND you can change it without much thinking (although in bash this could end up in sadness).

To use, just add to your .bashrc. The ZSH and OSX versions are available in the original blogpost!

I’ve updated it to:

export MARKPATH=$HOME/.marks
test -d $MARKPATH || (
    echo "Creating dir MARKPATH=$MARKPATH ..."
    mkdir $MARKPATH
)
function jump {
    if [[ "$1" != "" ]]; then
        cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
    else
        echo "Usage: $0 <mark>"
    fi
}
function mark {
    mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
function unmark {
    if [[ "$1" != "" ]]; then
        rm -i "$MARKPATH/$1"
    else
        echo "Usage: $0 <mark>"
    fi
}
function marks {
    ls -l "$MARKPATH" | sed 's/ / /g' | cut -d' ' -f9- | sed 's/ -/\t-/g' | grep -v '^$'
}

_completemarks() {
    if [[ $COMP_CWORD -ne 1 ]]; then
        COMPREPLY=()
    else
        local curw=${COMP_WORDS[COMP_CWORD]}
        local wordlist=$(find $MARKPATH -type l -printf "%f\n")
        COMPREPLY=($(compgen -W '${wordlist[@]}' -- "$curw"))
    fi
    return 0
}
complete -F _completemarks jump unmark

Changes:

  • create directory if it does not exist
  • do not accept ‘jump’ and ‘unmark’ without argument
  • do not print empty lines in ‘marks’
  • autocomplete only first argument

TODO:

  • check if directory of mark is available



The Author

Older Ramblings

Tweets

Linux User Sig