Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to create a list of completions for a command based on some folder's files?

Say I want to run Sublime Text 2 on one of my projects, which are saved as ~/sublime_projects/*.sublime-project. Desired behaviour:

   foo bar project2

in case there are foo.sublime-project, bar.sublime-project, project2.sublime-project files in ~/sublime_projects.

share|improve this question
up vote 4 down vote accepted

You can use the following bash function, add it in you .bashrc

Note the directory path for PROJECTS_DIR variable

# bash completion for sublime projects in ~/projects
    local cur=${COMP_WORDS[COMP_CWORD]}
    cd ${PROJECTS_DIR}
    PROJECTS=$(ls ${LOOK_FOR})
    COMPREPLY=( $(compgen -W "${PROJECTS}" -- $cur) )
complete -F _subl subl

What it does:

  • when you type subl TAB TAB TAB it lists all projects at the projects directory

  • when you type subl ANY_LETTER TAB it auto completes the project name if the ANY_LETTER_TAB matches with project's first character.

For example if you have inside ~/projects the following: test.sublime-project and foo.sublime-project and type subl t TAB in terminal you will get subl test.sublime-project

UPDATE the script bellow does not change the PWD i am leaving both for future reference.

# bash completion for sublime projects in ~/projects
    local cur=${COMP_WORDS[COMP_CWORD]}
    # cd ${PROJECTS_DIR}
    PROJECTS=$(ls ${PROJECTS_DIR} | grep "^${cur}.*${LOOK_FOR}")
complete -F _subl subl

The second script looks for a typed character after subl and replaces it with the matched filename's full path. For example if there is a test.sublime-project in ~/projects by typing subl t will be replaced by subl /home/USERNAME/projects/test.sublime-project

share|improve this answer
Amazing, exactly what I was looking for. Thanks. – mreq Aug 24 '13 at 11:42
One thing: This changes your $PWD to $PROJECTS_DIR. Is there a way to keep the current one? I tried saving it and restoring after COMPREPLY assignment, but that didn't work. – mreq Aug 24 '13 at 11:50
Thanks for the edit! Unfortunately, the second script doesn't work with the blank <tab> completions of projects. Using the first one - it's strange, that typing cd - after the $PWD change was done doesn't get you back to where you were. – mreq Aug 24 '13 at 12:43
It won't get you back because the script returns. As for the second script, you can pass to subl wildcard * and TAB to list projects but not in a good looking way. Personally i prefer the first script. – Stef K Aug 24 '13 at 13:05

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.