Skip to content

Commit

Permalink
[shell] Extend powerful cd and move it to its own file
Browse files Browse the repository at this point in the history
Add support for ‘.../directory’ paths in the powerful cd and move it to
its own file.
  • Loading branch information
mina86 committed Jun 1, 2024
1 parent 4ada39b commit 79282b3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 48 deletions.
73 changes: 73 additions & 0 deletions bin/pcd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# shellcheck shell=sh disable=SC2164

cd() {
case "$#:$1" in
1:-h)
cat <<EOF
cd Move to home directory.
cd -P Resolve all symbolic links in \$PWD.
cd [-L | -P | --] <target> Move to given <target>.
cd [-L | -P | --] <target> <cmd> ... Run <cmd> in the given <target>.
<target> can be:
- Move to directory stored in \$OLDPWD
.../<path> Move to a sibling/cusing directory <path>; this will iterate
up the directories until <path> is located
<file> Move to directory containing the file
<dir> Move to given directory
EOF
;;
0:)
command cd
;;
1:-[LP])
command cd "$1" "${PWD}"
;;
1:*)
_cd_do -L "$1"
;;
2:--)
_cd_do -L "$2"
;;
2:-[LP])
_cd_do "$1" "$2"
;;
*)
if [ "$1" = -- ]; then
shift
set -- -L "$@"
elif [ "$1" != -P ]; then
set -- -L "$@"
fi
( _cd_do "$1" "$2" && shift 2 && exec "$@" )
esac
}

_cd_do() {
case "$2" in
-)
:
;;
.../*)
set -- "$1" "$2" .. "${2#*/}" "$(stat -Lc%d:%i /)"
while
if [ -e "$3/$4" ]; then
set -- "$1" "$3/$4"
if ! [ -d "$2" ]; then
set -- "$1" "$(dirname "$2")"
fi
false
else
test "$5" != "$(stat -Lc%d:%i "$3")"
fi
do
set -- "$1" "$2" "../$3" "$4" "$5"
done
;;
*)
if [ -e "$2" ] && ! [ -d "$2" ]; then
set -- "$1" "$(dirname "$2")"
fi
esac
command cd "$1" -- "$2"
}
54 changes: 6 additions & 48 deletions sh/shellrc
Original file line number Diff line number Diff line change
Expand Up @@ -138,54 +138,12 @@ if echo foo | grep --color=auto foo >/dev/null 2>/dev/null; then
fi

# Powerful cd
_cd_do() {
if [ x"$2" != x- ] && [ -e "$2" ] && ! [ -d "$2" ]; then
set -- "$1" "$(dirname "$2")"
fi
command cd "$1" -- "$2" || return
}

cd() {
case "$#:$1" in
1:-h)
cat <<EOF
cd
Move to home directory.
cd -P
Resolve all symbolic links in \$PWD.
cd [-P | --] (<dir> | <file> | -)
Move to <dir>, dirname(<file>), or \$OLDPWD; -P resolves all symlinks.
cd [-P | --] (<dir> | <file> | -) <command> [<args> ...]
Run <command> in the directory and then come back.
EOF
;;
0:|1:--)
command cd || return
;;
1:-P)
command cd -P "${PWD}" || return
;;
1:*)
_cd_do "-L" "$1"
;;
2:--)
_cd_do "-L" "$2"
;;
2:-P)
_cd_do "-P" "$2"
;;
*)
if [ "$1" = -- ]; then
shift
set -- -L "$@"
elif [ "$1" != -P ]; then
set -- -L "$@"
fi
( _cd_do "$1" "$2" && shift 2 && exec "$@" )
esac
}
# Bash interprets ’-=…’ as a flag so ‘--’ is needed but BusyBox complains about
# it so silence the warning.
if [ -e ~/.local/bin/pcd.sh ]; then
# shellcheck source=../bin/pcd.sh
. ~/.local/bin/pcd.sh
fi
# Bash interprets ’-=…’ as a flag so ‘--’ is needed
# but BusyBox complains about it so silence the warning.
alias -- -='cd -' 2>/dev/null

alias '+=git add'
Expand Down

0 comments on commit 79282b3

Please sign in to comment.