I am using zsh and oh-my-zsh on Ubuntu.

To change into the recent directory in the past there was an alias set to - which is the same as cd -. Somehow the alias disappeared at my machine. This might have happened due to updates I pulled from the oh-my-zsh repository.

Now, I would like to add this alias to my own dotfiles. How can I do this?


The alias was removed in this commit.

To add it back:

alias -- -='cd -'

Most of POSIX shells need -- for this alias work, only dash doesn't:

$ dash
$ alias -='echo 1'
$ -

Like with any other command, use -- to instruct the command that subsequent arguments beginning with - are not options.

alias -='cd -'

You may prefer to make - a function, to give it some use when it has arguments. In zsh, - is a precommand modifier, which runs a command with - prepended to the zeroth argument. (Granted, that's a little obscure, and you can use the ARGV0 variable to achieve the same effect.) Unlike alias, function is not a built-in command but a keyword that doesn't take options, so you can use function -.

function - {
  if [[ $# -eq 0 ]]; then
    cd "$OLDPWD"
    builtin - "$@"

(Or whatever you want - foo to do.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.