I've been fooling around with this for a while, and I've just gotten it to work the way I always wanted it. I've managed to create a set of functions that will let you cd directly into preset directories, and use bash_completion to quickly navigate subdirectories as well. Hopefully others will find this useful as well.
First, create a set of variables and simple functions like this in your bashrc:
Code:
export MUSICDIR="/mount/bigdrive/media/music"
cdmusic ()
{
cd "$MUSICDIR/$1" || cd "$MUSICDIR"
}
Now you can quickly cd into your music directory with "cdmusic", and further into subdirectories with e.g. "cdmusic rock/pink_floyd". But it won't autocomplete the directory listings for you...yet.
To enable bash_completion for the command, create a new file in /etc/bash_completion.d/ and name it something like 10_cd2dirs. For each function you've defined above, include the following code:
Code:
_cdmusic()
{
local CD_PATH="$MUSICDIR"
local IFS=$'\t\n' cur=`_get_cword` i j k
# Enable -o filenames option, see Debian bug #272660
compgen -f /non-existing-dir/ >/dev/null
local -r mark_dirs=$(_rl_enabled mark-directories && echo y)
local -r mark_symdirs=$(_rl_enabled mark-symlinked-directories && echo y)
# loop through CD_PATH contents
for i in ${CD_PATH}; do
# create an array of matched subdirs
k="${#COMPREPLY[@]}"
for j in $( compgen -d $i/$cur ); do
if [[ ( $mark_symdirs && -h $j || $mark_dirs && ! -h $j ) && ! -d ${j#$i/} ]]; then
j="${j}/"
fi
COMPREPLY[k++]=${j#$i/}
done
done
if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
i=${COMPREPLY[0]}
if [ "$i" == "$cur" ] && [[ $i != "*/" ]]; then
COMPREPLY[0]="${i}"
fi
fi
return 0
}
if shopt -q cdable_vars; then
complete -o nospace -v -F _cdmusic $nospace cdmusic
else
complete -o nospace -F _cdmusic $nospace cdmusic
fi
# Above code is a modified version of the
# bash_completion function for "cd", but this
# only lists entries in the directory defined
# in CD_PATH. I'm not 100% sure what all the
# commands do, but it seems to work for me.
Just be sure to modify the names on the first and final lines to match each function name, and make CD_PATH the main target directory of that function.
Now just reload your bashrc (or open a new console) and you should be able to cruise your directories in speed and comfort.