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

share|improve this question
    
Does the alias work for a single letter like x? or what about a number 1? –  remus Oct 30 '13 at 17:32

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks, this was exactly what I needed. –  OmnipotentEntity Oct 30 '13 at 22:36

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
}
share|improve this answer
    
why $(pwd) and not $PWD? –  gniourf_gniourf Oct 30 '13 at 18:26
    
because I use both of them so rarely I forget about the variable. –  glenn jackman Oct 30 '13 at 20:26

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.