-
Notifications
You must be signed in to change notification settings - Fork 8
/
to.sh
82 lines (77 loc) · 1.82 KB
/
to.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
78
79
80
81
82
# to
# Bookmark locations in bash
#
# Copyright (C) 2013 Mara Kim
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see http://www.gnu.org/licenses/.
TO_BOOKMARK_FILE=~/.bookmarks
TO_ECHO=echo
TO_CAT=cat
TO_PWD=pwd
TO_GREP=grep
TO_SED=sed
to() {
if [ "$1" ]
then
if [ "$1" = "-b" ]
then
if [ "$2" ]
then
# add bookmark
_to_rm "$2"
$TO_ECHO \>"$2" >> $TO_BOOKMARK_FILE
$TO_PWD >> $TO_BOOKMARK_FILE
fi
elif [ "$1" = "-r" ]
then
# remove bookmark
_to_rm "$2"
elif [ -a $TO_BOOKMARK_FILE ]
then
# go to bookmark if found
local TODIR=$($TO_GREP \>$1 $TO_BOOKMARK_FILE -A 1 -x | $TO_GREP -v \>$1)
if [ "$TODIR" ]
then
cd $TODIR
else
$TO_ECHO "No shortcut:" "$1"
fi
else
$TO_ECHO "No shortcut:" "$1"
fi
elif [ -a $TO_BOOKMARK_FILE ]
then
# show bookmarks
$TO_CAT $TO_BOOKMARK_FILE
fi
}
# remove bookmark
_to_rm() {
if [ -a $TO_BOOKMARK_FILE ]
then
local TODIR=$($TO_GREP \>$1 $TO_BOOKMARK_FILE -A 1 -x | $TO_GREP -v \>$1)
if [ "$TODIR" ]
then
$TO_SED /^\>$1\$/,+1d $TO_BOOKMARK_FILE > $TO_BOOKMARK_FILE~
mv $TO_BOOKMARK_FILE~ $TO_BOOKMARK_FILE
fi
fi
}
# tab completion
_to() {
local cur=${COMP_WORDS[COMP_CWORD]}
if [ -a $TO_BOOKMARK_FILE ]
then
COMPREPLY=( $(compgen -W "$($TO_GREP \> $TO_BOOKMARK_FILE | cut -c 2-)" -- $cur) )
fi
}
complete -F _to to