refactor as shell function
Authored by
mfwolffe <wolffemf@dukes.jmu.edu>
- SHA
21afd905965d1a62fb77e5221ef55c4b24fb2b97- Parents
-
e2b6586 - Tree
853b17b
21afd90
21afd905965d1a62fb77e5221ef55c4b24fb2b97e2b6586
853b17b| Status | File | + | - |
|---|---|---|---|
| M |
zsh/alias.zsh
|
4 | 1 |
| A |
zsh/function.zsh
|
58 | 0 |
zsh/alias.zshmodified@@ -14,6 +14,9 @@ alias zconfig="micro ${HOME}/.zshrc || ${EDITOR} ${HOME}/.zshrc" | ||
| 14 | 14 | # :::: navigation :::: |
| 15 | 15 | # ::::: :::::::::: ::::: |
| 16 | 16 | # |
| 17 | +# NOTE: this actually only worked in fish | |
| 18 | +# but I've rewritten `up` as a shell function | |
| 19 | +# found in $ZSH_CUSTOM/function.zsh | |
| 17 | 20 | # NOTE: the short circuit wont work in bash |
| 18 | 21 | # and (may??) not ALWAYS work in zsh (or fish) |
| 19 | 22 | # |
@@ -25,7 +28,7 @@ alias zconfig="micro ${HOME}/.zshrc || ${EDITOR} ${HOME}/.zshrc" | ||
| 25 | 28 | # which will correctly change dirs to ../../../ |
| 26 | 29 | # |
| 27 | 30 | alias bk="- || cd -" |
| 28 | -alias up="../ || cd ../" | |
| 31 | +# alias up="../ || cd ../" | |
| 29 | 32 | alias goclass="cd ${HOME}/Documents/Class/" |
| 30 | 33 | alias goproj="cd ${HOME}/Documents/Project/" |
| 31 | 34 | alias goorg="cd ${HOME}/Documents/GithubOrgs/" |
zsh/function.zshadded@@ -0,0 +1,58 @@ | ||
| 1 | +# defines a function named "up" that: | |
| 2 | +# - counts how many arguments ($#) were passed | |
| 3 | +# - does `cd ..` exactly that many times plus one | |
| 4 | +# | |
| 5 | +# In other words: | |
| 6 | +# up → $# = 0 → 1 parent | |
| 7 | +# up up → $# = 1 → 2 parents | |
| 8 | +# up up up → $# = 2 → 3 parents | |
| 9 | +# | |
| 10 | +# This pattern applies to `bk` too | |
| 11 | +# NOTE: no it doesn't | |
| 12 | +# | |
| 13 | + | |
| 14 | +up() { | |
| 15 | + # $# is “number of additional words” you typed | |
| 16 | + local n | |
| 17 | + n=$(( $# + 1 )) # if no args, n=1; if “up up”, $#=1→n=2; etc. | |
| 18 | + | |
| 19 | + # Ensure all arguments are exactly "up" | |
| 20 | + for arg in "$@"; do | |
| 21 | + if [[ "$arg" != "up" ]]; then | |
| 22 | + echo "Error: 'up' only accepts repeated 'up' arguments." >&2 | |
| 23 | + return 1 | |
| 24 | + fi | |
| 25 | + done | |
| 26 | + | |
| 27 | + # Calculate how many levels to go up | |
| 28 | + local n=$(( $# + 1 )) | |
| 29 | + while (( n > 0 )); do | |
| 30 | + builtin cd .. # use builtin cd to avoid recursing into this function | |
| 31 | + (( n-- )) | |
| 32 | + done | |
| 33 | +} | |
| 34 | + | |
| 35 | +# NOTE: does not function as intended | |
| 36 | +# | |
| 37 | +# bk() { | |
| 38 | +# # Ensure all arguments are exactly "bk" | |
| 39 | +# for arg in "$@"; do | |
| 40 | +# if [[ "$arg" != "bk" ]]; then | |
| 41 | +# echo "Error: 'bk' only accepts repeated 'bk' arguments." >&2 | |
| 42 | +# return 1 | |
| 43 | +# fi | |
| 44 | +# done | |
| 45 | + | |
| 46 | +# # Determine number of toggles: if no args, toggle once; else toggle once per "bk" | |
| 47 | +# local count | |
| 48 | +# if (( $# == 0 )); then | |
| 49 | +# count=1 | |
| 50 | +# else | |
| 51 | +# count=$# | |
| 52 | +# fi | |
| 53 | + | |
| 54 | +# while (( count > 0 )); do | |
| 55 | +# builtin cd - | |
| 56 | +# (( count-- )) | |
| 57 | +# done | |
| 58 | +# } | |