-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm.sh
More file actions
53 lines (52 loc) · 2.08 KB
/
m.sh
File metadata and controls
53 lines (52 loc) · 2.08 KB
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
#! /bin/sh
# copy from: https://zhuanlan.zhihu.com/p/57659856
function m() {
MARKPATH="${MARKPATH:-$HOME/.local/share/marks}"
[ -d "$MARKPATH" ] || mkdir -p -m 700 "$MARKPATH" 2> /dev/null
case "$1" in
+*) # m +foo - add new bookmark for $PWD
ln -snf "$(pwd)" "$MARKPATH/${1:1}"
;;
-*) # m -foo - delete a bookmark named "foo"
rm -i "$MARKPATH/${1:1}"
;;
/*) # m /bar - search bookmarks matching "bar"
find "$MARKPATH" -type l -name "*${1:1}*" | \
awk -F "/" '{print $NF}' | MARKPATH="$MARKPATH" xargs -I'{}'\
sh -c 'echo "{} ->" $(readlink "$MARKPATH/{}")'
;;
"") # m - list all bookmarks
command ls -1 "$MARKPATH/" | MARKPATH="$MARKPATH" xargs -I'{}' \
sh -c 'echo "{} ->" $(readlink "$MARKPATH/{}")'
;;
*) # m foo - cd to the bookmark directory
local dest="$(readlink "$MARKPATH/$1" 2> /dev/null)"
[ -d "$dest" ] && cd "$dest" || echo "No such mark: $1"
;;
esac
}
if [ -n "$BASH_VERSION" ]; then
function _cdmark_complete() {
local MARKPATH="${MARKPATH:-$HOME/.local/share/marks}"
local curword="${COMP_WORDS[COMP_CWORD]}"
if [[ "$curword" == "-"* ]]; then
COMPREPLY=($(find "$MARKPATH" -type l -name "${curword:1}*" \
2> /dev/null | awk -F "/" '{print "-"$NF}'))
else
COMPREPLY=($(find "$MARKPATH" -type l -name "${curword}*" \
2> /dev/null | awk -F "/" '{print $NF}'))
fi
}
complete -F _cdmark_complete m
elif [ -n "$ZSH_VERSION" ]; then
function _cdmark_complete() {
local MARKPATH="${MARKPATH:-$HOME/.local/share/marks}"
if [[ "${1}${2}" == "-"* ]]; then
reply=($(command ls -1 "$MARKPATH" 2> /dev/null | \
awk '{print "-"$0}'))
else
reply=($(command ls -1 "$MARKPATH" 2> /dev/null))
fi
}
compctl -K _cdmark_complete m
fi