Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Scrolling through directory stack in zsh

Steve Talley wrote:
> Does zsh support the ability to cycle through the directory stack, in
> the same way that it can cycle through the history list?
> Currently I have the up and down arrows bound to scroll through the
> history list.  What I'd like is to have the left and right arrows, if
> the line is blank, to scroll through the directory stack.  Just like
> the history list, each time I hit the right or left arrow, the
> directory would appear on the command line.  And if I hit enter at that
> point, then zsh will cd to that directory.

You can get at least some of the effect of that using the new zle
widget code available from 3.1.2.  The hard bit is overloading the
cursor keys so that they still work as normal, I haven't tried that.
However, the following (using ^x^n and ^x^p) will insert previous or
subsequent directory entries on the command line, and if you have
AUTO_CD set hitting return will take you there.  You may want either
to set AUTO_PUSHD as well, or to make the shell insert a 'pushd'
before the directory name (change the BUFFER= part), or the cd will
wipe out the first entry in the stack.

Note that these functions will simply wipe out anything on the command
line when called.  Some test like '[[ -n $BUFFER ]] && zle push-line'
might be sensible.

show-prev-dir() {
  (( dir_no )) || dir_no=1;
  local dirs
  [[ $((--dir_no)) < 1 ]] && dir_no=$#dirs
show-next-dir() {
  (( dir_no )) || dir_no=0;
  local dirs
  [[ $((++dir_no)) > $#dirs ]] && dir_no=1
zle -N show-next-dir
zle -N show-prev-dir
bindkey "^X^p" show-prev-dir
bindkey "^X^n" show-next-dir

Peter Stephenson <pws@xxxxxx>       Tel: +39 50 911239
WWW:  http://www.ifh.de/~pws/
Gruppo Teorico, Dipartimento di Fisica
Piazza Torricelli 2, 56100 Pisa, Italy

Messages sorted by: Reverse Date, Date, Thread, Author