I find the command completion tools in a shell indispensable (i.e. when you press Tab to complete a path or show you the options for a command - this works for svn, for example). Plus I get quite annoyed when they don't work for a command I'm using. I found out today it is pretty easy to add your own completions using the complete command (at least, when using Bash under Linux).
So here, for example, is how to get Rake to auto-complete with the names of the tasks in your Rake file:
complete -W "$(rake -T | awk 'NR != 1 {print $2}')" rake
It's not flawless, but it's a damn sight better than having to do rake -T and scrolling through a wordy list. NB this only works if you run the complete command in a directory where you have a Rake file to start with. There's probably some switch to complete which dynamically generates the completions when you try to use them: I need to investigate.
You could also try something like this to put up a list of names of hosts in your /etc/hosts file when you're using SSH:
complete -W "$(cat /etc/hosts | awk '$1 != "#" {print $2}')" ssh
You can just add these to your Bash profile to get them activated when starting a new shell, I imagine.
Comments
Enhance to the above
Hi,
Adding the following syntax to your bashrc will dynamically add and remove the completions as appropriate.
cd () {
= 1 {print $2}')" cap
command cd "$@";
# Add enhanced completion for a folder containing a rails app and remove it when we leave the folder
if [ -f ./Rakefile ]; then
complete -W "$(rake -T | awk 'NR
else
complete -r cap
fi
}
Hope this helps you out.
Some of the text has been
Some of the text has been eaten above... Essentially the if condition to check if there is a rake file before installing completions on entering a folder is the important part. Also removing the completions on exit is handled by the "else" and the -r.
I've also included a completion for capistrano that I use.
I'll paste again below in the hopes that it makes it this time.
cd () {
command cd "$@";
# Add enhanced completion for a folder containing a rails app and remove it when we leave the folder
if [ -f ./Rakefile ]; then
complete -W "SAME AS YOURS above" rake
else
complete -r rake
fi
if [ -f ./Capfile ]; then
complete -W "$(cap -T | grep '#' | awk 'NR != 1 {print $2}')" cap
else
complete -r cap
fi
stripe ;
}
Try Pastie for sharing code
Pastie is great for sharing code. It formats, and highlights.
Thanks, I know about Pastie.
Thanks, I know about Pastie. Can it be used as a repository and then accessed via an API?
Thanks for the tips. Useful.
Thanks for the tips. Useful.