From: Dominik Vogt <vogt@linux.vnet.ibm.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Help with directory switching functions
Date: Tue, 24 Nov 2015 11:32:39 +0100 [thread overview]
Message-ID: <20151124103239.GA31057@linux.vnet.ibm.com> (raw)
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
_RECORDED_DIR="$PWD"
fi
_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
_RECORDED_DIR="$OLDPWD"
_RECORDED_DIR_A="$_OLDPWD_A"
_IS_CWD_INTERESTING="0"
fi
}
function chpwd () {
_swapdir_chpwd
}
function _swapdir_precmd () {
test ! "$HISTCMD" = "$_CHPWD_N" && _IS_CWD_INTERESTING="1"
}
function precmd () {
_swapdir_precmd
}
function _swapdir () {
_IS_CWD_INTERESTING=1
cd "$_RECORDED_DIR"
_IS_CWD_INTERESTING=1
}
alias -- +-=_swapdir
-- snip --
Problems:
1. Command lines with multiple command cannot be handled properly
because precmd is just called once for the line, not for each
command.
$ 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.
Ciao
Dominik ^_^ ^_^
--
Dominik Vogt
IBM Germany
next reply other threads:[~2015-11-24 10:42 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-24 10:32 Dominik Vogt [this message]
2015-11-28 18:54 ` Bart Schaefer
2015-11-30 10:48 ` Dominik Vogt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20151124103239.GA31057@linux.vnet.ibm.com \
--to=vogt@linux.vnet.ibm.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).