-
Notifications
You must be signed in to change notification settings - Fork 1
/
bookmarker.sh
93 lines (76 loc) · 2.42 KB
/
bookmarker.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
83
84
85
86
87
88
89
90
91
92
93
#!/bin/sh
# Set of bash functions that allows you to bookmark folders in the command-line.
# To bookmark a folder, simply go to that folder, then bookmark it like so:
# bookmark foo
#
# The bookmark will be named "foo"
#
# When you want to get back to that folder use:
# go foo
#
# To see a list of bookmarks:
# bookmarksshow
#
# Tab completion works, to go to the shoobie bookmark:
# go sho[tab]
#
# Your bookmarks are stored in the ~/.bookmarks file
bookmarks_file=~/.bookmarks
# Create bookmarks_file it if it doesn't exist
if [[ ! -f $bookmarks_file ]]; then
touch $bookmarks_file
fi
bookmark (){
bookmark_name=$1
if [[ -z $bookmark_name ]]; then
bookmark_name=${PWD##*/} #Store the current folder name as bookmark by default
fi
bookmark="`pwd`|$bookmark_name" # Store the bookmark as folder|name
if [[ -z `grep "$bookmark_name$" $bookmarks_file` ]]; then
echo $bookmark >> $bookmarks_file
echo "Bookmark '$bookmark_name' saved"
else
echo "Bookmark with '$bookmark_name' already exists"
echo 'Please provide another name for your bookmark. For example:'
echo ' bookmark foo'
fi
}
unbookmark (){
unbookmark_name=$1
if [[ -z $unbookmark_name ]]; then
echo 'Invalid name, please provide a name that has to be unbookmarked. For example:'
echo ' unbookmark foo'
else
unbookmark="$unbookmark_name$" # Store the bookmark as folder|name
if [[ -z `grep "$unbookmark" $bookmarks_file` ]]; then
echo "Bookmark not present, so can't be unbookmarked. To see a list of bookmarks:"
echo " bookmarksshow"
else
`grep -v "$unbookmark" $bookmarks_file > .bookmark_temp; mv .bookmark_temp $bookmarks_file`
echo "Bookmark '$unbookmark_name' removed"
fi
fi
}
# Show a list of the bookmarks
bookmarksshow (){
cat ~/.bookmarks | awk '{ printf "%-40s%-40s%s\n",$1,$2,$3}' FS=\|
}
go(){
bookmark_name=$1
bookmark=`grep "|$bookmark_name$" "$bookmarks_file"`
if [[ -z $bookmark ]]; then
echo 'Invalid name, please provide a valid bookmark name. For example:'
echo ' go foo'
echo
echo 'To bookmark a folder, go to the folder then do this (naming the bookmark 'foo'):'
echo ' bookmark foo'
else
dir=`echo "$bookmark" | cut -d\| -f1`
cd "$dir"
fi
}
_go_complete(){
# Get a list of bookmark names, then grep for what was entered to narrow the list
cat $bookmarks_file | cut -d\| -f2 | grep "$2.*"
}
complete -C _go_complete -o default go