diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2017-01-02 15:50:52 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2017-01-02 15:50:52 +1300 |
commit | ca20e3e759d168a9f4dacdb05dad2a2b295f458b (patch) | |
tree | 687b7c6c983933e0e7fa04c8a631dbc7a0386e58 | |
parent | Remove an unnecessary trailing slash from ad() (diff) | |
download | dotfiles-ca20e3e759d168a9f4dacdb05dad2a2b295f458b.tar.gz dotfiles-ca20e3e759d168a9f4dacdb05dad2a2b295f458b.zip |
Add marked-dir tools
-rw-r--r-- | README.markdown | 6 | ||||
-rw-r--r-- | sh/shrc.d/gd.sh | 18 | ||||
-rw-r--r-- | sh/shrc.d/md.sh | 21 | ||||
-rw-r--r-- | sh/shrc.d/pmd.sh | 8 | ||||
-rw-r--r-- | sh/shrc.d/xd.sh | 24 |
5 files changed, 77 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown index 777d5dd1..94bf42bb 100644 --- a/README.markdown +++ b/README.markdown @@ -164,6 +164,12 @@ terminals. If a function can be written in POSIX `sh` without too much hackery, I put it in `sh/shrc.d` to be loaded by any POSIX interactive shell. Those include: +* Four functions for using a "marked" directory, which I find a more + manageable concept than the `pushd`/`popd` directory stack: + * `md()` marks a given (or the current) directory. + * `gd()` goes to the marked directory. + * `pmd()` prints the marked directory. + * `xd()` swaps the current and marked directories. * `ad()` is a `cd` shortcut accepting targets like `/u/l/b` for `/usr/local/bin`, as long as they are unique. * `bc()` silences startup messages from GNU `bc(1)`. diff --git a/sh/shrc.d/gd.sh b/sh/shrc.d/gd.sh new file mode 100644 index 00000000..754bd8ff --- /dev/null +++ b/sh/shrc.d/gd.sh @@ -0,0 +1,18 @@ +# Go to marked directory +gd() { + + # Refuse to deal with unwanted arguments + if [ "$#" -gt 0 ] ; then + printf >&2 'gd(): Unspecified argument\n' + return 2 + fi + + # Complain if mark not actually set yet + if ! [ -n "$PMD" ] ; then + printf >&2 'gd(): Mark not set\n' + return 2 + fi + + # Go to the marked directory + cd -- "$PMD" +} diff --git a/sh/shrc.d/md.sh b/sh/shrc.d/md.sh new file mode 100644 index 00000000..cf44abb3 --- /dev/null +++ b/sh/shrc.d/md.sh @@ -0,0 +1,21 @@ +# Set marked directory to given dir or current dir +md() { + + # Accept up to one argument + if [ "$#" -gt 1 ] ; then + printf >&2 'md(): Too many arguments\n' + return 2 + fi + + # If first arg unset or empty, assume the user means the current dir + [ -n "$1" ] || set -- "$PWD" + + # If specified path not a directory, refuse to mark it + if ! [ -d "$1" ] ; then + printf >&2 'md(): Not a directory\n' + return 2 + fi + + # Save the specified path in the marked directory var + PMD=$1 +} diff --git a/sh/shrc.d/pmd.sh b/sh/shrc.d/pmd.sh new file mode 100644 index 00000000..03f18b7b --- /dev/null +++ b/sh/shrc.d/pmd.sh @@ -0,0 +1,8 @@ +# Print the marked directory +pmd() { + if ! [ -n "$PMD" ] ; then + printf >&2 'pmd(): Mark not set\n' + return 2 + fi + printf '%s\n' "$PMD" +} diff --git a/sh/shrc.d/xd.sh b/sh/shrc.d/xd.sh new file mode 100644 index 00000000..01b8fd3a --- /dev/null +++ b/sh/shrc.d/xd.sh @@ -0,0 +1,24 @@ +# Swap current directory with marked directory +xd() { + + # Refuse to deal with unwanted arguments + if [ "$#" -gt 0 ] ; then + printf >&2 'gd(): Unspecified argument\n' + return 2 + fi + + # Complain if mark not actually set yet + if ! [ -n "$PMD" ] ; then + printf >&2 'gd(): Mark not set\n' + return 2 + fi + + # Put the current and marked directories into positional params + set -- "$PMD" "$PWD" + + # Try to change into the marked directory + cd -- "$1" || return + + # If that worked, we can swap the mark, and we're done + PMD=$2 +} |