1

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?

2 Answers 2

3

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

p() {
  cd ~/Projects/"$1"
}
2
  • 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, 2010 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. May 12, 2010 at 13:17
1

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)

You must log in to answer this question.

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