3

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?

2 Answers 2

7

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'
$ -
1
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"
  else
    builtin - "$@"
  fi
}

(Or whatever you want - foo to do.)

You must log in to answer this question.

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