blob: 80ae7c12490ac773a01f39f66ac9cad6d7cf840f (
plain) (
blame)
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
|
#
# sd -- sibling/switch directory -- Shortcut to switch to another directory
# with the same parent, i.e. a sibling of the current directory.
#
# $ pwd
# /home/you
# $ sd friend
# $ pwd
# /home/friend
# $ sd you
# $ pwd
# /home/you
#
# If no arguments are given and there's only one other sibling, switch to that;
# nice way to quickly toggle between two siblings.
#
# $ cd -- "$(mktemp -d)"
# $ pwd
# /tmp/tmp.ZSunna5Eup
# $ mkdir a b
# $ ls
# a b
# $ cd a
# pwd
# /tmp/tmp.ZSunna5Eup/a
# $ sd
# $ pwd
# /tmp/tmp.ZSunna5Eup/b
# $ sd
# $ pwd
# /tmp/tmp.ZSunna5Eup/a
#
# Seems to work for symbolic links.
#
sd() {
set -- "$(
# Check argument count
if [ "$#" -gt 1 ] ; then
printf >&2 'sd(): Too many arguments\n'
exit 1
fi
# Set the positional parameters to either the requested directory, or
# all siblings of the current directory if no request
spec=$1
set --
if [ -n "$spec" ] ; then
set -- "$@" ../"$spec"
else
for sib in ../.* ../* ; do
case ${sib#../} in
.|..|"${PWD##*/}") continue ;;
esac
set -- "$@" "$sib"
done
fi
# We should have exactly one sibling
case $# in
1) ;;
0)
printf >&2 'sd(): No siblings\n'
exit 1
;;
*)
printf >&2 'sd(): More than one sibling\n'
exit 1
;;
esac
# Print the target
printf '%s\n' "$1"
)"
# If the subshell printed nothing, return with failure
[ -n "$1" ] || return
# Try to change into the determined directory
command cd -- "$@"
}
|