6 ms·
In bash you can probably use "pushd" instead of "cd" (it builds a stack and can swap top two entries, as well as rotate the stack) and abuse "dirs" (to create t
by Adj_and_Styles 3y ago
In bash you can probably use "pushd" instead of "cd" (it builds a stack and can swap top two entries, as well as rotate the stack) and abuse "dirs" (to create the variables with something like "c3=$( dirs +3 2>/dev/null )").
- kazinator 3y agoI've used the pushd stuff for 30 years, but this system I just came up with is way more ergonomic. 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'
- JNRowe 3y agopushd also gives you simple access via `~-n` in both bash and zsh out of the box. The bonus being that `~-[n]` is correctly handled when used as an argument to other commands too, so that `cp myfile ~-4/` does what you'd expect. The zsh completion for cd functions really nicely for this too, given that you can use zstyle to annotate your entries or group dynamic completions separately. 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.
- kazinator 3y ago~-2 is shorter than messing with the dirs command but it does not improve over $c2. It does not rescue the whole directory stack misfeature which has no reason for existing as built-in C code inside Bash, with dedicated expansion syntax. It interferes with existing syntax, too; according to POSIX, that refers to the home directory of a user named -2. 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.
- JNRowe 3y agoPersonally, I'd find the completion output from `~-<Tab>` a worthy improvement, although admittedly with zsh you can configure completion to display both name and value for `$c<Tab>` too I guess(can't recall if that is possible with bash). 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_chap03.html#tag_03_437 https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1...
- kazinator 3y agoI'm noticing that in Bash, when I complete on ~-1 without a trailing slash, nothing happens. When I use $c1 and hit tab, it adds the slash for me! So in that situation, $c1 saves a keystroke. Well, not a keystroke per se, but having to use a different character rather than repeated Tab. 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!