oh-my-bash
oh-my-bash copied to clipboard
lib/directories: Add `cd` function, a clone of Zsh cd builtin command
The 1 .. 9 aliases in lib/directories.sh
work now.
$ cd aa
$ cd bb
$ cd cc
$ d
1 ~/cc
2 ~/bb
3 ~/aa
$ 3
$ pwd
~/aa
$ d
1 ~/aa
2 ~/cc
3 ~/bb
Seems insane to overwrite coreutils function can you reference the zsh merge request?
cd
command is a builtin command for Bash & Zsh.
The Zsh cd
command supports -1
, -2
these number options, as its manual explains:
cd [ -qsLP ] [ arg ] cd [ -qsLP ] old new cd [ -qsLP ] {+|-}n ...... The third form of cd extracts an entry from the directory stack, and changes to that directory. An argument of the form
+n' identifies a stack entry by counting from the left of the list shown by the dirs command, starting with zero. An argument of the form
-n' counts from the right. If the PUSHD_MINUS option is set, the meanings of+' and
-' in this context are swapped.
The Bash cd
command doesn't support this kind of number options, so these aliases actually don't work in Bash, alias 2='cd -2'
, alias 3='cd -3'
...
I really like this Zsh feature, it's very convenient.
The cd
function I wrote doesn't break any behavior of the default Bash cd
command, just removes the duplicate paths in the directory stack after switching directories, because Bash won't clean up for us.
@Kreyren @chaifeng This required a lot of testing, I think the first version needs to make it compatible must be a POXIS-shell.
@Kreyren @chaifeng This required a lot of testing, I think the first version needs to make it compatible must be a POXIS-shell.
Why do we need POXIS-shell on bash frontent?
I have rebased it on top of the latest master.
I added further adjustments c45db79, 998c4c0, and a583e3c and squashed. I also edited commit message styles.
Currently, the existing aliases 2
..9
defined in lib/directories.sh
do not work. This PR tries to support it. As far as I see the implementation and try a little, this cd
function seems to be designed very well. I would like to merge this PR later.