I want to cd to some directory within /long/path/name/. I would like a function cd2
that
- Takes an argument
cd
s to /long/path/name/$argument- And autocompletes from
cd2 <tab><tab>
as if I had runcd /long/path/name/; cd <tab><tab>
It is the third requirement that I am having trouble with. I can get a function to autocomplete only the subdirectories of /long/path/name/, but I cannot continue tabbing to get subdirectories of that subdirectory.
I would like to autocomplete as if I had typed cd
in /long/path/name/. The ideal would be to easily adapt _cd
to do the job, but that may not be possible.
I have been using the code below, but I have to admit that I am new to bash autocomplete and do not fully understand the _cdFromDir
syntax.
function cd2() {
cd /long/path/name/$@ ;
}
_cdFromDir() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(ls /long/path/name/)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _cdFromDir cd2