-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoing_places.plugin.zsh
81 lines (70 loc) · 1.75 KB
/
going_places.plugin.zsh
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
#! /bin/zsh
FAVPATH=~/.favorites
function _get_favloc(){
local a=`grep "^$1:" $FAVPATH`
if [ $a ]; then
a=("${(s/:/)a}")
local dst="$a[2]"
if [ $2 ]; then
dst="${dst}/$2"
fi
if [ -d $dst ]; then
echo "$dst"
return 0
else
return 1
fi
else
return 1
fi
}
function goto(){
local loc="$(_get_favloc $@)"
if [ $loc ]; then
cd "$loc"
return 0
else
return 1
fi
}
function favadd(){
local fav_name="${1:-$(basename "$PWD")}"
# Check for illegal characters in the favorite name
if [[ "$fav_name" =~ [^a-zA-Z0-9_] ]]; then
echo "favadd: $fav_name contains illegal characters" 1>&2
return 1
fi
# Check if the favorite already exists
if grep -q "^$fav_name:" "$FAVPATH"; then
echo "favadd: $fav_name already exists" 1>&2
return 1
fi
# Add the favorite
echo "$fav_name:$(pwd)" >> "$FAVPATH"
echo "favadd: $fav_name added to favorites" 1>&2
}
function favrm(){
# Set default favorite name to current directory's base name if no argument is provided
local fav_name="${1:-$(basename "$PWD")}"
# Check if the favorite exists
if grep -q "^$fav_name:" "$FAVPATH"; then
# Remove the favorite
sed -i -r "/^$fav_name:/d" $FAVPATH
echo "favrm: $fav_name removed from favorites" 1>&2
else
# Print an error if the favorite does not exist
echo "favrm: $fav_name does not exist on favorites list" 1>&2
return 1
fi
}
function favbu(){
if [ $1 ]; then
cp $FAVPATH $1
else
cp $FAVPATH ~/favorites_bu
fi
}
function favrestore(){
cp -f $1 $FAVPATH
}
alias favlist='sort $FAVPATH | sed "s/:/:-- /g" | column -t -s :'