Alias for directory using a symbol

1

0

I'm looking to set up an alias for my work directory, which can change as I change context, and I was trying to find something that was short enough to stand in as a replacement for ~. I'd be tickled pink if it's possible to have a one character name for the alias. Like cd % or something? Assigning an alias to % doesn't seem to work though.

I've tried looking for requirements for variable names, but I couldn't find any. I don't know if this is a shell built-in, I can't have been the first person to want to do this.

I'm using zsh, but I tagged bash in case it's something possible in both shells.

OmnipotentEntity

Posted 2013-10-30T16:58:07.297

Reputation: 1 322

Does the alias work for a single letter like x? or what about a number 1? – brandonscript – 2013-10-30T17:32:28.207

Answers

2

zsh provides several facilities to get what you want, but the closest I could come up with is:

alias -g %=/path/to/whereever

Which will substitute that path for % anywhere inside a command. You could also try:

alias '%'='cd /path/to/whereever'

Now % is a command to change directly to that directory. There is also

hash -d work=/path/to/whereever

Which would let you use ~work to refer to that directory.

wingedsubmariner

Posted 2013-10-30T16:58:07.297

Reputation: 1 432

1

I can't speak to zsh, but for bash, aliases are only used for commands not arguments.

You probably want to write a function:

cdw () {
    case $(pwd) in 
        */some/dir)       cd ../work ;;
        */some/other/dir) cd ~/tmp ;;
    esac
}

glenn jackman

Posted 2013-10-30T16:58:07.297

Reputation: 18 546

why $(pwd) and not $PWD? – gniourf_gniourf – 2013-10-30T18:26:57.553

because I use both of them so rarely I forget about the variable. – glenn jackman – 2013-10-30T20:26:42.637