aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d/gt.sh
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-03-29 10:50:26 +1300
committerTom Ryder <tom@sanctum.geek.nz>2017-03-29 10:50:26 +1300
commit162f108b44470d34d2dec149b295c6d2a8356d6d (patch)
tree640e7197c822efdf3be77d290fb0031a3f64c868 /sh/shrc.d/gt.sh
parentUpdate submodules (diff)
downloaddotfiles-162f108b44470d34d2dec149b295c6d2a8356d6d.tar.gz
dotfiles-162f108b44470d34d2dec149b295c6d2a8356d6d.zip
Add gt() (go to)
Diffstat (limited to 'sh/shrc.d/gt.sh')
-rw-r--r--sh/shrc.d/gt.sh28
1 files changed, 28 insertions, 0 deletions
diff --git a/sh/shrc.d/gt.sh b/sh/shrc.d/gt.sh
new file mode 100644
index 00000000..d18a4ab8
--- /dev/null
+++ b/sh/shrc.d/gt.sh
@@ -0,0 +1,28 @@
+# If the argument is a directory, change to it. If it's a file, change to its
+# parent. Stands for "get to".
+gt() {
+
+ # Check argument count
+ if [ "$#" -gt 1 ] ; then
+ printf >&2 'gd(): Too many arguments\n'
+ return 2
+ fi
+
+ # Strip trailing slash
+ set -- "${1%/}"
+
+ # If target doesn't have a leading slash, add PWD prefix
+ case $1 in
+ /*) ;;
+ *) set -- "${PWD%/}"/"$1"
+ esac
+
+ # If target isn't a directory, chop to its parent
+ [ -d "$1" ] || set -- "${1%/*}"
+
+ # If target is now empty, go to the root
+ [ -n "$1" ] || set -- /
+
+ # Try to change into the determined directory
+ command cd -- "$@"
+}