Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # in ~/.zshrc
- alias uu='u 2'
- alias uuu='u 3'
- # u
- # u CMD [ARG...]
- # u INTEGER
- # u INTEGER CMD [ARG...]
- # u DOTS e.g. `u ....` goes up four directories
- # u DOTS CMD [ARG...]
- function u {
- local count where
- if [[ $# = 0 ]]; then
- # `u`
- cd ..; return 0
- fi
- if [[ $1 =~ /^[1-9][0-9]*$/ ]]; then
- # `u INTEGER [CMD [ARG...]]`
- where=$(_u_cds $1); shift 1
- else if [[ $1 =~ /^\.+$/ ]]; then
- # `u DOTS [CMD [ARG...]]`
- where=$(_u_cds ${#1}; shift 1
- else
- # `u CMD [ARG...]`
- where=.
- fi
- if [[ $# = 0 ]]; then
- # `u [INTEGER | DOTS]`
- # change cwd and return
- cd $where; return $?
- fi
- # `u [INTEGER | DOTS] CMD [ARG...]
- # run $@ from the directory; don't change our cwd
- (cd $where && $@); return $?
- }
- function _u_cds {
- local count=$1 where=.
- for ((i = 0; i < $count; i++)); do where=$where/..; done
- # strip leading ./
- [[ $where != . ]] && where=${where:2}
- echo $where
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement