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