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?

Rodreegez
  • 133
  • 5

2 Answers2

3

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

p() {
  cd ~/Projects/"$1"
}
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)

Carlo
  • 11