I have all my code projects inside ~/Development/[subject]_dev/[project_folder]
So for example my super-secret projects on mutants > X-Weapon is at:
~/Development/mutants_dev/X-weapon
I'd like to change in my project directory and open the project in TextMate from everywhere (any pwd) in my Shell.
Currently I have this in my .bashrc:
dev() { cd ~/Development/$1_dev/$2 && mate .; }
With this i type dev mutants X-Weapon and I start coding in TextMate.
However I'm not completely satisfied (I'm picky sometimes) and I would like to have TAB autocomplete for $1 and $2. In other words i would like to hit tab and have the shell scope-search into my technologies (a.k.a folders inside Development) and then on my projects.
So for example:
dev mu[TAB] becomes dev mutants and then dev mutants X[TAB] becomes... You get it.
Is it possible? How can i set the TAB context? THX
You need to write your own completion function. Here is an example, which you can add to your bashrc.
_dev()
{
cur=${COMP_WORDS[COMP_CWORD]}
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $(compgen -W "$(ls ~/dev)" -- $cur ) )
elif [ $COMP_CWORD -eq 2 ]; then
prev=${COMP_WORDS[COMP_CWORD-1]}
COMPREPLY=( $(compgen -W "$(ls ~/dev/$prev)" -- $cur ) )
fi
}
dev() {
cd ~/dev/$1/$2 && mate .;
}
complete -F _dev dev
How it works:
When you type dev [TAB][TAB], the _dev() completion function is invoked. If you are trying to complete the first argument, it runs ls in the dev directory to get a list of all projects. If you are trying to complete the second argument, it runs ls within the project's directory.
For more information take a look at this tutorial: Writing your own Bash Completion Function
Use BDSM 'project' and project edit' functions for workflow navigation
Install bdsm as root (it installs to /usr/local/bdsm/) then in your user's profile,
EDITOR=mate ; export EDITOR
projects_path="$HOME/Development" ; export projects_path
# This loads the interactive bdsm shell functions like 'p' and 'pe'
# which reflect on the set $projects_path and EDITOR environment variables
if [[ -s "/usr/local/bdsm/modules/shell/project/interactive" ]]
source "/usr/local/bdsm/modules/shell/project/interactive"
fi
Then in your terminal session you can quickly switch to the project,
~ $ p mu X
~/Development/mutants_dev/X-weapon/ $
OR, alternatively, you can both switch in the terminal and launch your EDITOR:
~ $ pe mu X
~/Development/mutants_dev/X-weapon/ $ # mate was also launched on the current directory
Note that it does prefix based matching, so the first matching prefix found first in the projects_path will be what is used. This is nice as it can greatly reduce typing. For example when I am working on BDSM itself I have ~/projects/bdsm/core and /extensions and I can respectively do,
~$ p bd c
~$ p bd e
(I cannot use simply 'p b c' here as I have a ~/projects/bash/ directory as well and that prefix would match first.)
p() and pe() should work in both bash/zsh, if it does not please let me know in #beginrescueend on irc.freenode.net.
Enjoy,
~Wayne
References - https://bdsm.beginrescueend.com/bdsm/install/ - https://bdsm.beginrescueend.com/development/ # no need for core dev setup in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With