Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .