-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmark
executable file
·144 lines (122 loc) · 4.1 KB
/
mark
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
read -r -d '' usage << EOM
mark [-d] [mark_name [mark_destination]]
mark -l
Set a symlink bookmark at \$HOME/.mk/mark_name that directs to the current directory or mark_destination.
DESCRIPTION
Bookmarks are stored as symlinks (shortcuts) in \$HOME/.mk, and can be accessed like usual:
mv ~/.mk/mark_name1/somefile ~/path/to/destination
cd -P ~/.mk/mark_name
Use -P flag when cd into symlink to resolve dot-dot. Otherwise, your pwd will look like
~/.mk/mark_name instead of ~/some/directory/mark_name, and when you try to go back with
cd .. you will end up in the bookmark directory. You can set an alias in bashrc for
convenience
alias cd="cd -P"
Bookmarks are stored as symlinks, so moving, deleting, etc. will only apply to the bookmark.
When not given any arguments, set default mark \$mk/a -> cwd
OPTIONS
-d
Delete bookmark with mark_name instead of normal functionality
-l
List all bookmarks available
CONFIGURATIONS
bashrc:
export MK="\$HOME/.mk" # avoid typing out ~/.mk
alias cd="cd -P" # Automatically resolves symlinks with cd
You can also bookmark the ~/.mk folder in most GUI file explorers for easy access with GUI
as well. On Ubuntu, this is done by dragging the folder onto the sidebar.
EOM
# Note: use `-P` flag when `cd` into symlink to resolve dot-dot. Otherwise, your pwd will look
# like ~/.mk/mark_name instead of ~/some/directory/mark_name, and when you try to go back with
# `cd ..` you will end up in the bookmark directory
# Tip: you can bookmark the `.mk` folder in your file explorer to access it easily with GUI too.
# on Ubuntu, you do this by dragging the directory onto the sidebar.
#
# You can also export your mark folder as an environmental variable in bashrc:
# export MK="$HOME/.mk"
# This way, you can access your bookmarks with `$MK/mark_name`
# Enum indicating mode of command
# 0: new bookmark; 1: delete bookmark
readonly MODE_NEW=0;
readonly MODE_DEL=1;
readonly MODE_HELP=2;
readonly MODE_LIST=3;
mode=$MODE_NEW
while getopts "dhl" opt; do
case $opt in
d)
mode=$MODE_DEL
shift 1
;;
h)
mode=$MODE_HELP
shift 1
;;
l)
mode=$MODE_LIST
shift 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo "Try the -h flag for usage" >&2
exit 1
;;
esac
done
# Set bookmark directory where bookmarks (symlinks) are stored
mark_folder="$HOME/.mk"
if [[ ! -d "$mark_folder" ]]; then
mkdir "$mark_folder"
fi
mark_path="$mark_folder/${1:-a}"
mark_dest="$(pwd)/$2"
case $mode in
"$MODE_HELP" ) # Print usage
echo "$usage" >&2
;;
"$MODE_NEW" ) # Create new bookmark
# TODO: Bookmark Description <26-01-23, xydxydxyd1> #
# Validate input
if [[ "$#" -gt 2 ]]; then
echo "Illegal number of parameters" >&2
echo "Try the -h flag for usage" >&2
exit 1
fi
if [[ ! -e $mark_dest ]]; then
echo "$mark_dest does not exist. Aborting..."
return
fi
# Option for overriding existing bookmark
if [[ (-L $mark_path) && ($mark_path != "$mark_folder/a") ]]; then
read -r -p 'Mark already exists. Override (y/n)? ' yn
if [[ "$yn" != 'y' ]]; then
echo 'Aborting...'
exit 1
fi
fi
rm -f "$mark_path"
ln -s "$mark_dest" "$mark_path"
echo "Symlink created: $mark_path --> $mark_dest"
;;
"$MODE_DEL" ) # Delete bookmark
# Validate input
if [[ "$#" -ne 1 ]]; then
echo "Illegal number of parameters" >&2
echo "Try the -h flag for usage" >&2
exit 1
fi
if [[ ! -h "$mark_path" ]]; then
echo "$mark_path does not exist"
exit 1
fi
read -r -p "Remove $mark_path? " yn
if [[ "$yn" != 'y' ]]; then
echo "Aborting..."
fi
rm -f "$mark_path"
;;
"$MODE_LIST" ) # List all bookmarks
ls -l "$mark_folder"
;;
esac
exit 0