I've played with bash autocomplete now for a while, but couldn't find a solution for my problem. I have a project directory with subfolders like this:
- projects/ruby/project1
- projects/ruby/project2
- projects/rails/project3
- projects/html/project4
Now I want to have a command, call it cdproject where I can cd in any subfolder within my projects dir and subdirs. And this command should provide a autocomplete feature where I can type cdproject pr --> TAB TAB and then get a list like ruby/project1, ruby/project2, rails/project3...
My problem is how to handle the subdirs. The programm cdproject looks like this
#!/bin/bash
cd $1
The cdproject-autocompletion.bash looks like this
_cdproject()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(ls ~/Dropbox/projects)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -o default -o nospace -F _o o
And inside my .bash_profile I've sourced the cdproject-autocompletion.bash with
source ~/cdproject-autocompletion.bash
So anyone an idea, how to achieve this? Maybe the opts should return the subdir structure ruby/project1 but then the autocompletion should only work on the last part, the actual project name. And I have no idea how this is possible.
cd ~/Dropbox/projects/$1. But I can't figure out how to include the subdirectories, so that I can directly switch into one subdir. Every project has a different name, maybe thats important