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...
Also, I can shorten these variables to one letter, like $p, $q, ...
Bash has an option so that tab completion will expand out the directory.
shopt -s direxpand
this is useful because variables like $c1 or syntax like ~-1 entered into the history is not useful for recall.I know about M-x C-e to expand everything, but that is far too clumsy compared to just the tab completion doing it.
Here is something: the direxpand option is not working for ~-n/[Tab] completions. Only for variable names!
$ cd ~-
0 -- ~
1 -- ~/Downloads
2 -- ~/Desktop
The handling of tilde expansion under bash is a readline option, so users would configure it in their inputrc. It is pretty useful in general, because then it is also available to tools like imv¹ beyond just bash. Unfortunately, as it is handled separately from word completion you'd need to change the completion function to make it match the mark-directories behaviour you like.If I wanted the text expanded for history use I'd probably expand it before writing using a zshaddhistory hook, but I don't think there is an equivalent in bash so changing the line buffer probably is the best option.