-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell-bookmarks.sh
executable file
·77 lines (65 loc) · 2.37 KB
/
shell-bookmarks.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
BOOKMARK_DIR="$HOME/.shell-bookmarks"
# create bookmark directory if it does not exists
[ -d "$BOOKMARK_DIR" ] || mkdir "$BOOKMARK_DIR"
if [ -d "$BOOKMARK_DIR" ]; then
export CDPATH=".:$BOOKMARK_DIR:/"
function warp {
# $1: bookmark name
[ "$#" -ne "1" ] && echo 'Usage: warp $bookmarkName' && return 1
[ ! -L "$BOOKMARK_DIR/$1" ] && echo "Bookmark named $1 not found" && return 1
cd -P "$BOOKMARK_DIR/$1"
}
# warp completion function
_warp() {
local IFS=$'\n'
local cur=${COMP_WORDS[COMP_CWORD]}
local BOOKMARK_LIST
BOOKMARK_LIST="$(/bin/ls "$BOOKMARK_DIR")"
COMPREPLY=( $( compgen -W "$BOOKMARK_LIST" -- "${cur}") )
} && complete -F _warp warp
function bookmark {
# $1: directory that will be bookmarked
# $2: bookmark name
[ "$#" -ne "2" ] && echo 'Usage: bookmark $directory $bookmarkName' && return 1
[ ! -d "$1" ] && echo "$1 is not an absolute path to a directory" && return 1
[ -L "$BOOKMARK_DIR/$2" ] && echo "bookmark name $2 already in use" && return 1
ln -s "$1" "$BOOKMARK_DIR/$2"
}
function unbookmark {
# $@: space separated bookmark names
[ "$#" -lt "1" ] && echo 'Usage: unbookmark $bookmarkName...' && return 1
for bookmarkName in "$@"
do
if [[ -L "$BOOKMARK_DIR/$bookmarkName" ]]; then
rm "$BOOKMARK_DIR/$bookmarkName"
else
echo "No bookmark named $bookmarkName has been found"
fi
done
}
# unbookmark completion function
_unbookmark() {
local IFS=$'\n'
local cur=${COMP_WORDS[COMP_CWORD]}
local BOOKMARK_LIST
BOOKMARK_LIST="$(/bin/ls "$BOOKMARK_DIR")"
COMPREPLY=( $( compgen -W "$BOOKMARK_LIST" -- "${cur}") )
} && complete -F _unbookmark unbookmark
function rnbookmark {
# $1: bookmark name
# $2: new bookmark name
[ "$#" -lt "2" ] && echo 'Usage: rnbookmark $bookmarkName $newBookmarkName' && return 1
[ ! -L "$BOOKMARK_DIR/$1" ] && echo "Bookmark named $1 not found" && return 1
[ -L "$BOOKMARK_DIR/$2" ] && echo "bookmark name $2 already in use" && return 1
mv "$BOOKMARK_DIR/$1" "$BOOKMARK_DIR/$2"
}
# rnbookmark completion function
_rnbookmark() {
local IFS=$'\n'
local cur=${COMP_WORDS[COMP_CWORD]}
local BOOKMARK_LIST
BOOKMARK_LIST="$(/bin/ls "$BOOKMARK_DIR")"
COMPREPLY=( $( compgen -W "$BOOKMARK_LIST" -- "${cur}") )
} && complete -F _rnbookmark rnbookmark
fi