oh-my-bash icon indicating copy to clipboard operation
oh-my-bash copied to clipboard

lib/directories: Add `cd` function, a clone of Zsh cd builtin command

Open chaifeng opened this issue 4 years ago • 4 comments

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

chaifeng avatar Aug 26 '19 15:08 chaifeng

Seems insane to overwrite coreutils function can you reference the zsh merge request?

Kreyren avatar Aug 27 '19 12:08 Kreyren

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.

chaifeng avatar Aug 27 '19 14:08 chaifeng

@Kreyren @chaifeng This required a lot of testing, I think the first version needs to make it compatible must be a POXIS-shell.

nntoan avatar Aug 29 '19 05:08 nntoan

@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?

Kreyren avatar Aug 29 '19 14:08 Kreyren

I have rebased it on top of the latest master.

akinomyoga avatar Apr 09 '23 12:04 akinomyoga

I added further adjustments c45db79, 998c4c0, and a583e3c and squashed. I also edited commit message styles.

akinomyoga avatar Apr 09 '23 13:04 akinomyoga

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.

akinomyoga avatar Apr 09 '23 13:04 akinomyoga