Bash autocomplete for different directories

5

1

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.

23tux

Posted 2012-12-06T18:55:03.827

Reputation: 213

The question is old, but here is an update: For now, I use the Z Shell (zsh) along with oh-my-zsh and the plugin z. It provides a pretty good autocomplete feature. See here https://github.com/rupa/z, https://github.com/robbyrussell/oh-my-zsh

– 23tux – 2015-07-21T08:02:38.680

Doesn't answer your question directly, but something you can look into for changing directories quickly is: https://github.com/rupa/z

– sampson-chen – 2012-12-06T18:58:50.480

What is the behavior you are currently getting? Does it work as is without sub dirs? – terdon – 2012-12-09T00:24:44.827

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 – 2012-12-10T15:47:14.487

Would you consider simply renaming the folders to 1project, 2project etc? That's what I'd do :P – Cobolt – 2013-03-08T20:22:16.407

Answers

4

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"

Note: some people find it useful to include .. in CDPATH.

Unfortunately CDPATH doesn't (always) 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 isn't too bad.

You may have better success with dirs, pushd, and popd. These take some time to get the hang of. Note that pushd and popd can take a numeric parameter.

Also:
cd - returns you to the previous directory. Repeating the command toggles between the two most recent directories.
____________
A lot of documents (including man pages) show . (dot; i.e., the current directory) as a component in CDPATH.  However, at least some shells seem to act as though CDPATH ends with :., so, if you say cd foo, you will get ./foo unless some other parent directory listed in your CDPATH contains a foo.  YMMV.

Editor's note: Cygwin seems to support CDPATH autocomplete out-of-the-box (running bash version "4.1.17(9)-release").

Sherwood Botsford

Posted 2012-12-06T18:55:03.827

Reputation: 199

0

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
  git=$HOME/git/firefly/

  # last arg so far
  cur="${COMP_WORDS[COMP_CWORD]}"

  # 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

rplatel

Posted 2012-12-06T18:55:03.827

Reputation: 1