Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

cd $1

The cdproject-autocompletion.bash looks like this

  local cur prev opts
  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.

share|improve this question

migrated from stackoverflow.com Dec 7 '12 at 8:10

This question came from our site for professional and enthusiast programmers.

Doesn't answer your question directly, but something you can look into for changing directories quickly is: github.com/rupa/z –  sampson-chen Dec 6 '12 at 18:58
What is the behavior you are currently getting? Does it work as is without sub dirs? –  terdon Dec 9 '12 at 0:24
It now works for one folder, because my script just does a 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 –  23tux Dec 10 '12 at 15:47
Would you consider simply renaming the folders to 1project, 2project etc? That's what I'd do :P –  Cobolt Mar 8 '13 at 20:22

2 Answers 2

Reading deep into the question you are looking for smoother ways to navigate the file tree from the command line.

Option 1: Use CDPATH?

E.g.: CDPATH=".:~:~/projects/ruby:~/projects/rail:~/projects/html"

Although most of the time I find it sufficient to define it to be ~:.:..

Unfortunately CDPATH doesn't support autocompletion. There are ways to extend bash to do this. Google with terms CDPATH and autocompletion.

Admittedly this is a bit clunky. If there is a small set of parent directories you use this is isn't too bad.

You may have better success with dirs pushd and popd

These take some time to get the hang of. Remember that pushd and popd can take a numeric parameter

cd - returns you to the previous directory. Repeating the command toggles between two directories

share|improve this answer

This seems ridiculously complex, but it's the solution I came up with:

function gcd {
  cd $HOME/git/firefly/$1

function _gcdcomplete()
  local cmd curb cur opts

  # base dir

  # last arg so far

  # dirname-esque, but makes "xyz/" => "xyz/" not "."
  curb=$(echo $cur | sed 's,[^/]*$,,')

  # get list of directories (use commened out line for dirs and files)
  # append '/' to directories
  # cmd="find $git$curb -maxdepth 1 -type d -printf %p/\n , -type f -print "
  cmd="find $git$curb -maxdepth 1 -type d -printf %p/\n "

  # remove base dir from list and remove extra trailing /s
  opts=$($cmd | sed s:$git:: | sed s://*$:/:)

  # generate list of completions
  COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  return 0;
complete -o nospace -F _gcdcomplete gcd
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.