-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbm
executable file
·64 lines (57 loc) · 1.45 KB
/
bm
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
#!/bin/bash
# INSTALL: source this file in your ~/.bashrc
#
# Bookmark directories to easily navigate there later
#
# Usage:
# $ bm [command] [arguments]
#
# Commands:
# bm [path]
# bm cd index
# bm clear [index]
# bm list
#
# Positional Arguments:
# * path: Filepath to bookmark, defaults to current directory
# * index: Number of bookmark to navigate to or clear
# Main launcher --------------------------------------------------------------
bm () {
cmd=$1
if type "bm__$cmd" >/dev/null 2>&1; then
shift
"bm__$cmd" "$@"
else
bookmark=${1:-$(pwd)}
if [[ ! -d "$bookmark" ]]; then
echo "$bookmark is not a directory"
else
echo "$bookmark" >> "$HOME/.bookmarks"
cat "$HOME/.bookmarks" | sort | uniq >> "$HOME/.bookmarks.tmp"
mv "$HOME/.bookmarks.tmp" "$HOME/.bookmarks"
fi
fi
}
# Individual commands --------------------------------------------------------
bm__cd() {
if [ -f "$HOME/.bookmarks" ]; then
dest=$(cat $HOME/.bookmarks | sed -n "$1p")
cd $dest
else
echo "No existing bookmarks, create one with 'bm [path]'"
fi
}
bm__clear() {
if [ -z "$1" ]; then
rm "$HOME/.bookmarks"
else
sed -i "$1d" "$HOME/.bookmarks"
fi
}
bm__list() {
if [ -f "$HOME/.bookmarks" ]; then
cat -n "$HOME/.bookmarks"
else
echo "No existing boomarks, create one with 'bm [path]'"
fi
}