help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cdlog: alternative to using the pushd stuff.


From: Lawrence Velázquez
Subject: Re: cdlog: alternative to using the pushd stuff.
Date: Thu, 18 Jan 2024 22:57:08 -0500
User-agent: Cyrus-JMAP/3.9.0-alpha0-1374-gc37f3abe3d-fm-20240102.001-gc37f3abe

On Thu, Jan 18, 2024, at 9:41 PM, Kaz Kylheku wrote:
> I came up with a very simple, ergonomic system for recording directories
> where you "cd" to, and reference them.
>
> This shows we don't need dedicated code in the shell executable to
> have this kind of feature.

Sure, but bash already implements the feature.


> - A 9 element FIFO log is maintained of recent directories.
> - This is stored in variables c1 through c9.
> - Registers c1 to c4 are also known as x, y, z, w.

These are global variables with short names and are thus easy to
accidentally overwrite, even for someone who thinks they can remember
not to do so.  Conversely, a user who absentmindedly does something
as innocuous as

        $ . ./cdlog.sh

        [work work work]

        $ x='stash some data'
        $ cd working_dir

will find *their* data overwritten.


> - with direxpand enabled, $x/[Tab] will expand the variable contents.
>   (~-1/[Tab] will not).

This can be done with the tilde-expand Readline command (bound to
Meta-& by default), although it's admittedly not tab completion.

https://www.gnu.org/software/bash/manual/html_node/Miscellaneous-Commands.html#index-tilde_002dexpand-_0028M_002d_0026_0029


> - Variables can be exported, so a subshell easily sees the stack.

Subshells inherit the internal directory stack out of the box.

        bash-5.2$ cd /
        bash-5.2$ pushd /var
        /var /
        bash-5.2$ pushd /tmp
        /tmp /var /
        bash-5.2$ pushd /bin
        /bin /tmp /var /
        bash-5.2$ declare -p DIRSTACK
        declare -a DIRSTACK=([0]="/bin" [1]="/tmp" [2]="/var" [3]="/")
        bash-5.2$ (dirs; declare -p DIRSTACK)
        /bin /tmp /var /
        declare -a DIRSTACK=([0]="/bin" [1]="/tmp" [2]="/var" [3]="/")


-- 
vq



reply via email to

[Prev in Thread] Current Thread [Next in Thread]