To me Plone development environment means Fedora 12, terminator and vim. As I do lots of development in the shell there has been one annoying little thing which I'm tired of - cd:ing under development packages inner structure to where the actual code lives.
Doesn't seem much but if you repeat that many times a day you'll get tired of it. Of course you can always launch vim for the package root and open the file you wanted from there, but it's still extra effort.
Few weeks ago me and my colleague Jussi Talaskivi started to think that there has to be a script which takes you to your destination simply by parsing dotted name and using information from there to cd you to the correct place. After spending some time googling around we found none. Annoyed by this we decided to do this missing script by ourself.
Soon it turned out that this wasn't that easy task as it sounds - simple shell or python script where you just parse correct path from arguments wouldn't do the trick as they're launched in a separate process which doesn't have any effect on the working dir of your shell. Little research and try - error approach later we found working solution.
1. Create shell script (eg. pcd.sh) to somewhere on your $PATH which contains following lines:
2. Chmod your shell script so that you can execute it.
3. Edit your .bashrc and add following alias:
Now pcd command should work as expected:
Excellent!
The key element is the first dot in the alias. This makes your command to execute in the current shell session. As alias doesn't cope with arguments you'll need a script (in this case pcd.sh) for parsing to work. I know this isn't the most sophisticated piece of code and there probably are better ways to achieve the same result, but I couldn't find one.
I hope this example is useful for someone frustrated for the long nested structures we're all so familiar with.
Doesn't seem much but if you repeat that many times a day you'll get tired of it. Of course you can always launch vim for the package root and open the file you wanted from there, but it's still extra effort.
Few weeks ago me and my colleague Jussi Talaskivi started to think that there has to be a script which takes you to your destination simply by parsing dotted name and using information from there to cd you to the correct place. After spending some time googling around we found none. Annoyed by this we decided to do this missing script by ourself.
Soon it turned out that this wasn't that easy task as it sounds - simple shell or python script where you just parse correct path from arguments wouldn't do the trick as they're launched in a separate process which doesn't have any effect on the working dir of your shell. Little research and try - error approach later we found working solution.
1. Create shell script (eg. pcd.sh) to somewhere on your $PATH which contains following lines:
#!/bin/sh
cd `echo -n $1/;echo $1 | sed -e 's/\./\//g'`
2. Chmod your shell script so that you can execute it.
3. Edit your .bashrc and add following alias:
alias pcd=". pcd.sh"
Now pcd command should work as expected:
$ pwd
/home/jutaojan/workspace/
$ pcd very.long.packagename
$ pwd
/home/jutaojan/workspace/very.long.packagename/very/long/packagename
Excellent!
The key element is the first dot in the alias. This makes your command to execute in the current shell session. As alias doesn't cope with arguments you'll need a script (in this case pcd.sh) for parsing to work. I know this isn't the most sophisticated piece of code and there probably are better ways to achieve the same result, but I couldn't find one.
I hope this example is useful for someone frustrated for the long nested structures we're all so familiar with.
Really nice idea.
ReplyDeleteAs for the the bash alias, you can implement this with a function call in your .bashrc :
function pcd() {
cd `echo -n $1/;echo $1 | sed -e 's/\./\//g'`
}
and that accepts arguments.
Thanks Jukka for the idea and davidjb for the comment.
ReplyDeleteI expanded it a bit to support packages with a 'src' directory inside.
function pcd()
{
PACKAGENAME=$1
cd $PACKAGENAME
if [ -d "src" ]
then
cd "src"
fi
cd `echo $PACKAGENAME | sed -e 's/\./\//g'`
}
Davidjb, thanks! Function call in shell is for sure the missing more sophisticated solution.
ReplyDeleteCsenger, thank you also for the important addition! I think this script is near reaching the perfection in here :)
Btw, Command-T is a great Vim plugin for opening files in deep directory structures:
ReplyDeletehttps://wincent.com/products/command-t
Take a look at autojump, IMHO beats everything above.
ReplyDeletehttps://github.com/joelthelion/autojump
Example session:
> pwd
/home/wouter
> j panel
> pwd
/opt/zope/plone-coredev-4.2/src/plone.app.controlpanel/plone/app/controlpanel