Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I can't for the life of me how to create an alias that will switch to a given project directory.

I keep all my projects in a folder called Projects i.e. ~/Project/blog ~/Project/whatever

I'd like to have an alias along the lines of p whatever that would equate to cd ~/Project/$1 where $1 is whatever is given to p.

I have tried various combinations of alias p="cd ~/Projects/\$1" with all the usual suspects for regex escaping but I can't quite get it.

Any ideas?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can't use positional arguments in aliases. Use a function instead.

p() {
  cd ~/Projects/"$1"
}
share|improve this answer
    
Totally works, but of course it won't autocomplete the project name. I'm guessing because it's a function and doesn't know about the underlying directory structure? Is there a way to make it autocomplete? –  Rodreegez May 12 '10 at 12:55
    
You'd have to use bash's programmable completion feature. See the Programmable Completion subsection of the READLINE section of the bash(1) man page. –  Ignacio Vazquez-Abrams May 12 '10 at 13:17

Maybe this is what you were looking for: how to expand aliases inline in bash?

so a simple solution would be using the shell-expand-line command

Define the alias without \$1

 $bash>alias p="cd ~/Projects/"
 $bash>p 

just press Meta-Ctrl-e to expand getting :

$bash>cd /home/USERNAME/Projects/

This solution puts an awkward blank space at the end of the expansion that you'll have to remove with BackSpace before writing whatever else.
As Ignacio Vazquez-Abrams suggested a more elegant solution wolud be using a function with autocomplete properties (looking at /etc/bash_completion)

share|improve this answer

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.