I have a simple pair of functions which I use to go up a level based on typing the portion of the path I want to go up until:
# When called as `argu string`, prints the closest ancestor directory which
# contains "string". If none is found, or "string" is empty, the current
# working directory is printed. This function is useful if you need to specify
# a file as an argument, but the relative path to it involves moving several
# directories up from the current directory. For example, instead of doing:
# $ pwd
# /this/is/a/very/long/directory/path/for/demonstration/purposes/
# $ ls ../../../../another/directory/something.log
# You could do:
# $ ls `argu dir`/another/directory/something.log
argu() {
if [[ "$1" == "" ]]; then
echo "`pwd`/"
return
fi
echo "`pwd | sed "s/^\(.*"$1"[^/]*\)\/.*$/\1/g"`/"
}
# When called as `cdu string`, changes directories into the closest ancestor
# directory of the current directory which contains "string". If none is found,
# or "string" is empty, no changes are made to the current directory.
cdu() {
cd `argu $1`
}
I also put the following in ~/.zsh/completion/_cdu so I can get tab-completion of the portions of the path:
#compdef cdu
_cdu_parents=$(pwd | tr '/' ' ')
_arguments "1:parents:($_cdu_parents)"
Or bash (in ~/.bash/completion/cdu):
# bash completion for cdu
_cdu()
{
local cur prev split=false
COMPREPLY=()
_get_comp_words_by_ref -n : cur prev
_dirs=$( pwd | tr '/' ' ' )
COMPREPLY=( $( compgen -W "$_dirs" -- "$cur" ) )
}
complete -F _cdu cdu
# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh