Created
September 16, 2014 15:29
-
-
Save andrewwheal/117a7968a854257d7655 to your computer and use it in GitHub Desktop.
cdup bash function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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