-
Notifications
You must be signed in to change notification settings - Fork 1
/
lcd.sh
100 lines (76 loc) · 2.65 KB
/
lcd.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
94
95
96
97
98
99
100
# Lazy Change Directory
#
# derived from some old code from spoonm, https://github.com/twerth/bashmarks and my own modifications.
bookmarks_file=~/.bookmarks
# create bookmark storage file.
if [[ ! -f $bookmarks_file ]]; then
touch $bookmarks_file
fi
# add a bookmark. ######################################################################################################
acd ()
{
bookmark_name=$1
# no bookmark name specified.
if [[ -z $bookmark_name ]]; then
echo "you didn't specify a bookmark name sir."
else
# store the bookmark as name|path.
bookmark="$bookmark_name|`pwd`"
# ensure bookmark doesn't already exist.
if [[ -z `grep "^$bookmark_name|" $bookmarks_file` ]]; then
echo $bookmark >> $bookmarks_file
echo "bookmark saved: $bookmark_name"
else
echo "bookmark already exists by that name."
fi
fi
}
# delete a bookmark. ###################################################################################################
dcd ()
{
bookmark_name=$1
# no bookmark name specified.
if [[ -z $bookmark_name ]]; then
echo "you didn't specify a bookmark name sir."
else
bookmark=`grep "^$bookmark_name|" "$bookmarks_file"`
# no match.
if [[ -z $bookmark ]]; then
echo "no bookmark by that name sir."
else
cat $bookmarks_file | grep -v "^$bookmark_name|" > $bookmarks_file.tmp
mv $bookmarks_file.tmp $bookmarks_file
echo "bookmark deleted: $bookmark_name"
fi
fi
}
# grep bookmarks. #####################################################################################################
gcd ()
{
needle=$1
cat $bookmarks_file | sort | awk '{ printf "%-30s%-40s%s\n",$1,$2,$3}' FS=\| | grep $needle
}
# print bookmarks. #####################################################################################################
pcd ()
{
cat $bookmarks_file | sort | awk '{ printf "%-20s%-40s%s\n",$1,$2,$3}' FS=\|
}
# lazy change directory. ###############################################################################################
lcd ()
{
bookmark_name=$1
bookmark=`grep "^$bookmark_name|" "$bookmarks_file"`
if [[ -z $bookmark ]]; then
echo "no bookmark by that name sir."
else
dir=`echo "$bookmark|" | cut -d\| -f2`
cd "$dir"
fi
}
# bash completion. #####################################################################################################
_go_complete()
{
cat $bookmarks_file | cut -d\| -f1 | grep "$2.*"
}
complete -C _go_complete -o default lcd
complete -C _go_complete -o default dcd