Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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:

 subl<tab><tab>
   foo bar project2

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

share|improve this question
add comment

1 Answer

up vote 3 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
_subl()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    PROJECTS_DIR="/home/mreq/projects/"
    LOOK_FOR="*.sublime-project"
    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
_subl()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    PROJECTS_DIR="/home/mreq/projects/"
    LOOK_FOR=".sublime-project"
    # cd ${PROJECTS_DIR}
    PROJECTS=$(ls ${PROJECTS_DIR} | grep "^${cur}.*${LOOK_FOR}")
    COMPREPLY=${PROJECTS_DIR}${PROJECTS}
}
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
add comment

Your Answer

 
discard

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.