Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have web projects in /var/www/projects/some/long/path/strange-project-name

Now I want to type in terminal:

webs str{TAB}

It should autocomplete to the webs strange-project-name (basing on ls /var/www/projects/some/long/path/) and after executing the command, the pwd should point to project path. Kind of smart cd strange-project-name with autocomplete

How would you implement this feature? Some smart alias? Function in .bashrc? Script?

Some smart alias?

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

You would need a two-parter. One, a completion script:

_webs() {
    local cur prev projdir=/var/www/projects/some/long/path

    COMPREPLY=()
    cur=$(_get_cword)
    prev=${COMP_WORDS[COMP_CWORD-1]}

    COMPREPLY=( $( compgen -W '$( command ls "$projdir/$cur*" | sed "s|$projdir/||")' -- '' ) )
}
complete -F _webs webs

_get_cword depends on the bash-completions package being installed. If you don't have it then change the line:

cur=$(_get_cword)

to:

cur=${COMP_WORDS[$COMP_CWORD]}

Two, a function:

webs () {
    local projdir=/var/www/projects/some/long/path
    cd "$projdir"
    do_something_ "$@"
}
share|improve this answer
    
Thanks. With this as a start I managed to do what I needed. –  takeshin Nov 6 '10 at 12:44
add comment

I'm not sure if this is what you need...

in terminal...

sudo vim /etc/inputrc

find this rows and uncomment it:

# alternate mappings for “page up” and “page down” to search the history

#”\e[5~”: history-search-backward

#”\e[6~”: history-search-forward

now you can auto complete commands from your history with page up/down

share|improve this answer
add comment

I would create a custom bash autocompletion script for that. Take a look at /etc/bash_completion.d directory and its numerous examples. Then create a file called webs to that directory and adapt it to your needs.

share|improve this answer
add comment

Correct me if I'm wrong, but I think this is exactly what Lightning was created for. It's a Ruby project, so this isn't a good answer if using Bash is a goal in itself.

share|improve this answer
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.