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

Help with directory switching functions

I'm trying to write a function that alternately switches between
two directories.  With "+-" I can repeatedly switch between the
current directory and the last one where I did anything (type any
command except switching directories).  There are still a few
rough edges that might be improved.

The function makes use of the chpwd and precmd functions.  chpwd
writes down the $HISTCMD of the last command that switched
directories, and precmd uses that information to mark the current
directory as one yuop might want to switch back later.  When such
a directory is left, chpwd finally stores its path in a variable
that is then used by "-+".

-- snip --
function _swapdir_chpwd () {
	local _PWD_A
	local _OLDPWD_A

	_CHPWD_N="$[HISTCMD + 1]"
	_PWD_A=$(readlink -f "$PWD")
	test x"$_PWD_A" = x"$_RECORDED_DIR_A"; then
	_OLDPWD_A=$(readlink -f "$OLDPWD")
	if test "$_IS_CWD_INTERESTING" = 1 -a ! x"$_PWD_A" = x"$_OLDPWD_A"; then
		# really leaving an interesting dir -> record it

function chpwd () {

function _swapdir_precmd () {
	test ! "$HISTCMD" = "$_CHPWD_N" && _IS_CWD_INTERESTING="1"

function precmd () {

function _swapdir () {

alias -- +-=_swapdir
-- snip --


1. Command lines with multiple command cannot be handled properly
   because precmd is just called once for the line, not for each

  $ cd ~; ls; cd ..

2. Depending on $HISTCMD is a bit hacky.  What I'd really want to
   do is to look at the command being executed and decide
   individually which commands are "interesting" enough to warrant
   recording the current directory.

3. I'd prefer a shell builtin instead of "readlink".

Any suggestions for improvement are welcome.


Dominik ^_^  ^_^


Dominik Vogt
IBM Germany

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