Skip to content

Instantly share code, notes, and snippets.

@andrewwheal
Created September 16, 2014 15:29
Show Gist options
  • Save andrewwheal/117a7968a854257d7655 to your computer and use it in GitHub Desktop.
Save andrewwheal/117a7968a854257d7655 to your computer and use it in GitHub Desktop.
cdup bash function
cdup() {
p=${1}
if [[ $p = -* ]]; then
c="cd "
while [[ $p -lt 0 ]]; do
c=$c"../"
let p=p+1
done
$c
else
a=$(pwd)
#remove current dir
b=${a%/*}
#build cd
c="cd "${b%$1*}$1
$c
fi
}
_cdup_complete() {
local cur prev path ar opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
path=$(pwd)
old_ifs=$IFS
IFS=/
ar=$path
opts=""
for x in $ar; do
opts=$opts$x" "
done
IFS=$old_ifs
COMPREPLY=( $(compgen -W "${opts}" ${cur}) )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment