2

Let's say I cd to /usr/local/bin but I want to change my pwd to /usr/bin, how can I do that with zsh? I know I can do things like cd /a/b/ and then cd b c in order to change my pwd to /a/b, but I can't quite figure out how to do what I desire.

2 Answers 2

2

Given the flexible functionality of zsh's cd, there are at least three ways to do what you desire. You can either write cd /usr/local /usr or cd local / or cd local/bin bin. One can probably come up with more solutions.

cd's syntax in zsh is cd OLD NEW. This command matches your $PWD with OLD and replaces any matching parts with NEW. You should keep in mind that cd works relative to your current directory, so if you write cd /usr/local usr while in /usr/local/bin, zsh will give you an error, because there is no such directory as /usr/local/bin/usr/bin. Always watch the slashes.

1

You can use the two-argument form of cd to replace a substring of the current working directory. For example, to remove /local, you can use

cd /local ''

Since multiple slashes don't make any difference, you can also write this as cd /local / (results in /usr//bin, then the double slash is simplified to a single one) or cd local / (also results in /usr//bin).

Another approach is to use command line edition. With the default settings, type cd $PWD on the command line, then press Tab: the variable substitution is replaced by the variable's value, and you can edit it to your leisure. Under setopt auto_cd, you don't need to write cd first. If your configuration causes Tab not to expand the variable, use whatever key invokes the zle function expand-or-complete rather than complete-word.

You must log in to answer this question.

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