407be8f0368379892f7c0d5a3994bfca74e06969

Author
Marc Cornellà <marc@mcornella.com>
Committer
Marc Cornellà <marc@mcornella.com>
Date

Message

feat(dirhistory): preserve forward directories with `cde` alias (#9328)

Closes #11954

Co-authored-by: Jeff Williams <jeffsmessages@gmail.com>

Diff

 1diff --git a/plugins/dirhistory/README.md b/plugins/dirhistory/README.md
 2index ede9b541010b88f6f5606a3aef9c0953cbbf90ad..66e3e04697b58fbc419f568d3aedeee9f3076036 100644
 3--- a/plugins/dirhistory/README.md
 4+++ b/plugins/dirhistory/README.md
 5@@ -60,3 +60,46 @@ to `/usr` again.
 6 After that, <kbd>Alt</kbd> + <kbd>Down</kbd> will probably go to `/usr/bin` if `bin` is the first directory in alphabetical
 7 order (depends on your `/usr` folder structure). <kbd>Alt</kbd> + <kbd>Up</kbd> will return to `/usr`, and once more will get
 8 you to the root folder (`/`).
 9+
10+### cde
11+
12+This plugin also provides a `cde` alias that allows you to change to a directory without clearing the next directory stack.
13+This changes the default behavior of `dirhistory`, which is to clear the next directory stack when changing directories.
14+
15+For example, if the shell was started, and the following commands were entered:
16+
17+```shell
18+cd ~
19+cd /usr
20+cd share
21+cd doc
22+
23+# <Alt + Left>
24+# <Alt + Left>
25+```
26+
27+The directory stack would look like this:
28+
29+```sh
30+➜  /usr typeset -pm dirhistory_\*
31+typeset -ax dirhistory_past=( /home/user /usr )
32+typeset -ax dirhistory_future=( /usr/share/doc /usr/share )
33+```
34+
35+This means that pressing <kbd>Alt</kbd> + <kbd>Right</kbd>, you'd go to `/usr/share` and `/usr/share/doc` (the "future" directories).
36+
37+If you run `cd /usr/bin`, the "future" directories will be removed, and you won't be able to access them with <kbd>Alt</kbd> + <kbd>Right</kbd>:
38+
39+```sh
40+➜  /u/bin typeset -pm dirhistory_\*
41+typeset -ax dirhistory_past=( /home/user /usr )
42+typeset -ax dirhistory_future=( /usr/bin )
43+```
44+
45+If you instead run `cde /usr/bin`, the "future" directories will be preserved:
46+
47+```sh
48+➜  /u/bin typeset -pm dirhistory_\*
49+typeset -ax dirhistory_past=( /home/user /usr /usr/bin )
50+typeset -ax dirhistory_future=( /usr/share/doc /usr/share )
51+```
52diff --git a/plugins/dirhistory/dirhistory.plugin.zsh b/plugins/dirhistory/dirhistory.plugin.zsh
53index 4650666be166ba158becc9b64f748fd6cf992d46..706bb6fb23bd0b83621206aab27008319c76aaea 100644
54--- a/plugins/dirhistory/dirhistory.plugin.zsh
55+++ b/plugins/dirhistory/dirhistory.plugin.zsh
56@@ -11,9 +11,10 @@ dirhistory_past=($PWD)
57 dirhistory_future=()
58 export dirhistory_past
59 export dirhistory_future
60-
61 export DIRHISTORY_SIZE=30
62 
63+alias cde='dirhistory_cd'
64+
65 # Pop the last element of dirhistory_past.
66 # Pass the name of the variable to return the result in.
67 # Returns the element if the array was not empty,