ajeetdsouza / zoxide

A smarter cd command. Supports all major shells.
MIT License
20.54k stars 523 forks source link

Import from `cd` history #703

Open moritztim opened 5 months ago

moritztim commented 5 months ago

I think it should at least be an option to train zoxide on the history of cd calls after installing it. I think it would even make sense to just do that by default.

Pyrestone commented 5 months ago

I built a simple python script that does this.

https://gist.github.com/Pyrestone/105c837f8aab14bd833d5807b2c43751

usage:

python zoxide_from_bash_history.py
#inspect the generated add_command.bash
bash ./add_command.bash

It certainly isn't perfect, and just tries to re-trace the history of cd in your .bash_history, to keep track of where you've been and how often. If a path is absolute, it will just go there. If it's relative, it will try to apply it to the last directory. If that fails, will just try to apply it on top of your home directory to see if that works (unless it's "..", so "/home" doesn't become your most-visited directory). Else, it will just ignore the path

In this way, it keeps track of all the places in your file system that you've likely been to, and on every change it saves it to a list of directories, which get added to a command which you can then run to add them all to your zoxide history at once.

moritztim commented 5 months ago

Wow you really went all the way down that rabbit hole and came back out with a result. I did not 😅

Pyrestone commented 5 months ago

TBH though, after doing that, I now hate that zoxide pollutes the bash_history with commands that are no longer valid normal cd commands. That's especially an issue when using "--cmd cd" in the zoxide init command.

Maybe I should make a feature request for a mode where zoxide replaces the entries it leaves in bash_history with the final cd command it actually executes in the end, so that the bash_history stays useful....

moritztim commented 5 months ago

That sounds like a good idea but I can imagine they wouldn't wanna do it because it's sorta nonstandard to manipulate the bash history, even if it's just an optional mode.

NamkhangNLe commented 4 months ago

This is really interesting.