Skip to content

Instantly share code, notes, and snippets.

@SammysHP SammysHP/jump.sh

Last active Jun 25, 2017
Embed
What would you like to do?
Bookmarking tool for Bash
####################################################################################
#
# NAME
# jump - Bookmark system for Bash
#
# SYNOPSIS
# jump [OPTION]... [BOOKMARK]...
#
# DESCRIPTION
# Easily create bookmarks to the current working directory and jump to
# stored bookmarks.
#
# This tool supports bash-completion.
#
# Bookmarks are stored in $XDG_DATA_HOME/bashjump. If $XDG_DATA_HOME is
# not set, it defaults to "~/.local/share". Bookmarks are stored as
# symbolic links and thus can be modified by hand easily.
#
# OPTIONS
# -d bookmark
# --delete bookmark
# Delete <bookmark>
#
# -c bookmark
# --create bookmark
# Create bookmark for the current working directory named <bookmark>.
#
# -l
# --list
# List all stored bookmarks and their target.
# This is the default action if no options or arguments are given.
#
# bookmark
# Jump to <bookmark>.
#
# INSTALLATION
# Source this file in your Bash configuration (e.g. ~/.bashrc).
#
# EXAMPLES
# Create bookmark "photos":
# $ cd /mnt/somedisk/media/photos
# $ jump -c photos
#
# Show all bookmarks:
# $ jump
#
# Jump to bookmark "photos":
# $ jump photos
#
# Jump to bookmark "photos" and subdirectory "wedding":
# $ jump photos/wedding
#
# Delete bookmark "photos":
# $ jump -d photos
#
# AUTHOR
# Sven Karsten Greiner <sven@sammyshp.de>
#
####################################################################################
function __bashjump_list {
for f in $BOOKMARK_DIR/*; do
if [[ -L "$f" ]]; then
printf '%-25s %s\n' "$(basename -- "$f")" "$(readlink -- "$f")"
fi
done
}
function __bashjump_delete {
NAME="${1///}"
if [[ -z "$NAME" ]]; then
echo "Missing argument!"
return
fi
TARGET="$BOOKMARK_DIR/$NAME"
if [[ -L "$TARGET" ]]; then
rm "$TARGET"
else
echo "Bookmark '$NAME' does not exist!"
return
fi
}
function __bashjump_goto {
NAME="${1%%/*}"
if [[ -z "$NAME" ]]; then
echo "Missing argument!"
return
fi
TARGET="$BOOKMARK_DIR/$NAME"
if [[ -L "$TARGET" ]]; then
cd -P "$BOOKMARK_DIR/$1"
else
echo "Bookmark '$NAME' does not exist!"
return
fi
}
function __bashjump_create {
NAME="${1///}"
if [[ -z "$NAME" ]]; then
echo "Missing argument!"
return
fi
TARGET="$BOOKMARK_DIR/$NAME"
if [[ ! -e "$TARGET" ]]; then
ln -s "$(pwd)" "$TARGET"
else
echo "Bookmark '$NAME' does already exist!"
return
fi
}
function jump {
[[ -z $XDG_DATA_HOME ]] && local XDG_DATA_HOME=$HOME/.local/share
local BOOKMARK_DIR=$XDG_DATA_HOME/bashjump
# The "if" is optional here as "mkdir -p" does nothing if it already exists.
if [[ ! -d $BOOKMARK_DIR ]]; then
mkdir -p $BOOKMARK_DIR
fi
case "$1" in
""|"-l"|"--list") __bashjump_list ;;
"-c"|"--create") __bashjump_create "$2" ;;
"-d"|"--delete") __bashjump_delete "$2" ;;
*) __bashjump_goto "$1" ;;
esac
}
function __bashjump_completion {
local cur prev words cword
_init_completion || return
[[ -z $XDG_DATA_HOME ]] && local XDG_DATA_HOME=$HOME/.local/share
local BOOKMARK_DIR=$XDG_DATA_HOME/bashjump
local opts="-l --list -c --create -d --delete"
if [[ $cword == 1 && $cur == -* ]]; then
COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
return
fi
[[ -d $BOOKMARK_DIR ]] || return
if [[ $cword == 2 && ("$prev" == "-d" || "$prev" == "--delete") ]]; then
local IFS=$'\n' tmp=( $( compgen -W "$( ls "$BOOKMARK_DIR" )" -- "$cur" ) )
COMPREPLY=( "${tmp[@]// /\ }" )
return
fi
if [[ $cword == 1 ]]; then
compopt -o nospace
cur="$BOOKMARK_DIR/$cur"
_filedir -d
COMPREPLY=( "${COMPREPLY[@]#$BOOKMARK_DIR/}" )
[[ ${#COMPREPLY[@]} -le 1 ]] && COMPREPLY=( "${COMPREPLY[@]/%/\/}" )
return
fi
}
complete -F __bashjump_completion jump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.