by kazinator · 2y ago · 0 comments
These could be interpolated into commands:
$ cd somewhere # found via CDPATH
/path/to/somewhere
$ cp $cd1/file.txt . # copy file from where we were just before the cd, to here
cd1 would be always the most recent, cd2 second most recent.Here it is up to cd4:
if [ -z "$c1" ] ; then
c4=
c3=
c2=
fi
cdlog_chdir()
{
local cur=$PWD
if command cd "$@" ; then
c4=$c3
c3=$c2
c2=$c1
c1=$cur
fi
}
cdlog_chdir()
{
local cur=$PWD
if command cd "$@" && [ "$PWD" != "$cur" ]; then
# only if we successfully change to a different
# directory do the following
if [ "$cur" == "$c2" ] && [ "$PWD" == "$c1" ] ; then
# Special case: if we changed to the directory that is second
# in the cdlog, then just swap between those two.
c2=$PWD
c1=$cur
else
# Otherwise rotate through all four.
c4=$c3
c3=$c2
c2=$c1
c1=$cur
fi
fi
}
Tip: in bash use ESC Ctrl-E to expand variables in the command line before running it. Then the command is recorded with concrete paths, rather than variables that are changing. You can recall the command from history to repeat it.Handy aliases:
alias cd='cdlog_chdir -P'
alias cs='cdlog_chdir "$c1"'
"cs" goes back to the previous; if that is repeated, it triggers the swapping behavior in cdlog_chdir, hence the "cs" name.The dirs command is a poorly considered interface to what should just be a handful of simple variables.
The whole feature is basically mistake, copied from tcsh in order to help tcsh users switch to Bash or something.
pushd with no arguments swaps between the most recent two. That's an important use case and I nailed that requirement with the "cs" alias.
I just added a function to cdlog_pop aliased to pd, which changes to $c1 and removes it, rotating the entries in the other direction. I extended the history to 9 items (with c5 to c9 being hidden entries not shown by cdlog).
if [ -z "$c1" ] ; then
c9=
c8=
c7=
c6=
c5=
c4=
c3=
c2=
fi
cdlog_chdir()
{
local cur=$PWD
if command cd "$@" && [ "$PWD" != "$cur" ]; then
# only if we successfully change to a different
# directory do the following
if [ "$cur" == "$c2" ] && [ "$PWD" == "$c1" ] ; then
# Special case: if we changed to the directory that is second
# in the cdlog, then just swap between those two.
c2=$PWD
c1=$cur
else
# Otherwise rotate through all
c9=$c8
c8=$c7
c7=$c6
c6=$c5
c5=$c4
c4=$c3
c3=$c2
c2=$c1
c1=$cur
fi
fi
}
cdlog_pop()
{
if [ -n "$c1" ] && command cd "$c1"; then
c1=$c2
c2=$c3
c3=$c4
c4=$c5
c5=$c6
c6=$c7
c7=$c8
c8=$c9
c9=
fi
}
cdlog()
{
# c5 through c9 are hidden
printf "c1: %s\n" "$c1"
printf "c2: %s\n" "$c2"
printf "c3: %s\n" "$c3"
printf "c4: %s\n" "$c4"
}
alias cs='cdlog_chdir "$c1"'
alias cd='cdlog_chdir -P'
alias pd='cdlog_pop'If you're using zsh you can implement the GPs idea by hooking chpwd, so that you don't need to change your muscle memory on cd. You can even combine them by enabling the auto_pushd option and just carrying on with regular cd whilst gaining access to `~-n` with no other changes.
We can "export c1" and the others to make them visible to child processes. That makes it possible to launch a subshell in which the parent shell's directory log is readily available.
Things that can be very effectively and ergonomically achieved with a bit of scripting in the user's environment should never be built into the command interpreter. That just adds to the executable size, documentation, and maintenance.
User names should not begin with a hyphen¹, so it shouldn't interfere with home directory references.
The child process point is an interesting one, I feel like you could workaround it by using a chpwd hook to export $dirstack and set it at startup(or a precmd function and $DIRSTACK if using bash). Something like `export DIRSTACK=${(pj#:#)dirstack}` in the hook and `dirstack=(${(ps#:#)DIRSTACK})` in the startup file. I'll concede this point though, as while a fun thought experiment the end result would be ghastly ;)
¹ https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1...