aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d/rd.sh
blob: 9633713a64f3785483fffbfa291084d5b3c94bca (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
# Replace the first instance of the first argument string with the second
# argument string in $PWD, and make that the target of the cd builtin. This is
# to emulate a feature of the `cd` builtin in Zsh that I like, but that I think
# should be a separate command rather than overloading `cd`.
#
#     $ pwd
#     /usr/local/bin
#     $ rd local
#     $ pwd
#     /usr/bin
#     $ rd usr opt
#     $ pwd
#     /opt/bin
rd() {

    # Check argument count
    case $# in
        1|2) ;;
        *)
            printf >&2 \
                'rd(): Need a string and optionally a replacement\n'
            return 2
            ;;
    esac

    # Check there's something to substitute, and do it
    case $PWD in
        *"$1"*)
            set -- "${PWD%%"$1"*}""$2""${PWD#*"$1"}"
            ;;
        *)
            printf >&2 'rd(): Substitution failed\n'
            return 1
            ;;
    esac

    # Try to change into the determined directory
    command cd -- "$1"
}