bd
bd copied to clipboard
add mode for tracking previous CWDs
Hey.
The current feature of bd
is to allow one to go back to any parent dir above the current working directory.
I'd propose another respectively additional mode:
-
bd
keeps a (configurable) array of last current working directories - since it's anyway
bash
, it could be easily added to thePROMPT_COMMAND
and from there readPWD
, respectively add it to some array (which should get some name, that hopefully nothing else uses (e.g.BASH_BD_PWD_RRDB
or something better ;-) ) . - the array would be a round robin array, so once it's longer than it's maximum lenght, the first element is unset, the indexing can be gotten back to by assigning the array to itself.
- if a
PWD
is to be added it's checked whether it's already in the array, if so, it's just moved to the end (similarly by unsetting the old one, "re-indexing" the array and then adding it newly (not doing this, if it was the last element) - one could make a mode, in which the PWD is resolved to the realpath (i.e. resolving symlinks)
- the last dir, which is the current working dir, wouldn't be shown by the tool
The interface could be as follows:
- either a new tool, which when completing shows the array index + path, one per line
- the number would then be use to select the desired entry and
bd
wouldcd
to it or: - one could integrate it directly into
bd
- since the numbers might be directories in the path (and could therefore overlap with the current functionality) the index number would need to be used with a leading
/
which cannot appear in the current arguments to it anyway
e.g.:
/home/foo/test/bar$ bd<tab>
0: /usr
1: /home/foo/test
2: /proc/2343/fd
home foo test
$
So the last line is what we have currently.
The other lines are the previous current working dirs.
If one now enters e.g. bd /0
it would cd to /usr
and then the list would look like:
0: /home/foo/test
1: /proc/2343/fd
2: /home/foo/test/bar
(and 0: /usr
would be hidden in the array already)
Another config option could allow to hide out any dirs in the list, which are parents of the current dir, since these are already quickly reachable via the last line, e.g. above:
/home/foo/test/bar$ bd<tab>
0: /usr
2: /proc/2343/fd
home foo test
$
1: /home/foo/test
wouldn't be printed, because it's already reachable via the test
.
Of course it cannot be deleted from the array, because if one moves to another dir, it would be needed again.
And just bd
could be made a shortcut for going to the last one in the list, so consecutive calls of just bd
should switch back and forth between the previous and current dir.
What do you think? ;-)
Cheers, Chris.
Hey @calestyo
Your idea is very interesting.. However I am not actively maintaining bd anymore and would like to keep it simple as it is.
You can however fork it and do whatever you want in that fork. :)
Cheers, Vigneshwaran
@calestyo So... like bd
, but with history?
I personally like to override cd
with a shell function that automatically pushes directories onto your Directory Stack if you're after that.
For Example:
cd() {
builtin cd "$@" # Calls original cd command
[ "$OLDPWD" != "$PWD" ] && pushd "$PWD" >/dev/null # Don't add the same dir to the stack twice in a row
}
This allows cd
to retain its existing behavior, but also use BASH's Directory Stack feature to track which directories you've been to. Directories can be listed with dirs -v
and you can use the index numbers presented by that command with cd
Generally with this setup using cd ~2
will take you back to your previous directory.
Or if you have shopt -s autocd
enabled, you can just do ~2