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

1 Response to “Improved bash directory bookmarking”


  1. 2015/02/09 at 23:58

    Mad article buddy. Like the effort.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


The Author

Older Ramblings

Tweets

Linux User Sig

%d bloggers like this: