Remix.run Logo
nickjj 3 days ago

I like using fzf combined with zsh's shell history filtering.

You can type `cd ~` and press CTRL+r to immediately fuzzy match commands you've run with `cd ~`. fzf naturally ranks paths to cd into on top. If you find that too noisy you can just hit CTRL+r with an empty prompt and then search for `^cd ~` to only find cd commands.

I've written about filtering related history with zsh here: https://nickjanetakis.com/blog/hooking-up-fzf-with-zsh-tab-c...

If you want to go into ultra lazy mode you can also type `cd ` and spam the up / down arrows to only show commands from your history where you cd'd into a directory. That use case is also covered in the above post. I normally don't use this for changing directories but it can be done.

jvanderbot 3 days ago | parent | next [-]

You don't even need history.

Just `find . -type d | fzf` to determine what dir to change to (or ~ for "anywhere else")

1. Make an alias fcd 2. Make a tab complete that does that for the command fcd

This is kind of 101 bash - just DIY.

Here's mine:

(2) is the hardest part - just write something that works with `complete` and fzf. Nowadays this is childs play for any AI to just spit out.

    fz_comp()
    {
      COMPREPLY=()
      local cur="${COMP_WORDS[COMP_CWORD]}"
      local prev="${COMP_WORDS[COMP_CWORD-1]}"
      local opts="none"
      if [ -z "$cur" ];then
        COMPREPLY=($(find $1 -type d | fzf --preview="ls {} -l"))
        return
      fi
      COMPREPLY=($(find $1 -type d | grep $cur | fzf --preview="ls {} -l"))
    }
(1) is just a) set the new command b) make the completion call c) map that call to <TAB> completion.

    alias fcd=cd
    _fcd(){ fz_comp $(pwd) }
    complete -F _fcd fcd
there you go.
BeetleB 3 days ago | parent [-]

If I'm reading this correctly, this will pass all subdirectories to fzf, which is very different from only directories you've visited.

jvanderbot 3 days ago | parent [-]

The premise is the same: Dump history into fzf and add a grep/awk.

My point was that requiring a new shell (or even history) is a limiting factor here, and either backwards search over commands (as suggested ITT), or just plan fzf directory changes are more functional and already integrated into bash.

robenkleene 3 days ago | parent [-]

`cd foo` is useless in history if you're not already in foo's parent directory. This is the problem Zoxide solves, `z foo` will still do something useful in that case. (Side note about fzf, recursively fuzzy finding subdirectories fine for some use cases, but it doesn't scale as well as Zoxide.)

jvanderbot 3 days ago | parent [-]

Yes, the marginal improvements from changing your entire shell are not to be disregarded. I'll change to asserting that it's entirely possible to do a nice 80/20 without changing shells.

robenkleene 3 days ago | parent [-]

What do you mean? Zoxide isn't a shell, it's just a CLI program. It doesn't involve changing shells, you can see the list of supported shells in the README (it's more comparable to fzf than a shell, it ships with optional shell integrations the same way fzf does).

jvanderbot 3 days ago | parent [-]

How did you get all the way down here and lose the fact that top level comment is about zsh and finding past commands, and also changing directories? Was it not you that brought up zsh?

And I that brought up bash examples to show how it might be done? We've gone around in circles lads back on the bus.

robenkleene 3 days ago | parent | next [-]

To me, the interesting conversation here is between these three workflows:

1. Recursive directory fuzzy-search

2. Searching history for `cd` commands

3. Zoxide-style matching visited directories

Which are all ways to make it easier to traverse directories.

For my part, the thrust of your argument seemed to be advocating for #1, so that's what I was responding to (since this is a thread on #3, I'm mainly focusing on why you'd use that instead of #1 or #2, but all three are workable).

All of these can be implemented in any of the common shells. I didn't see anyone making a case for a specific shell here, I think the mentions of a specific shell are incidental (e.g., what people happen to use).

BeetleB 2 days ago | parent [-]

Indeed - it's not even an either/or. I have both 1 and 3 as keybindings.

BeetleB 3 days ago | parent | prev [-]

He never said he changed shells - just that he uses zsh history. He was likely using zsh anyway.

jvanderbot 3 days ago | parent [-]

My logic (take it or leave it) is that since zsh is not the default distro nearly anywhere (Kali?), a randomly selected person who might read that advice (and subsequent advice ITT), would likely find "Use ZSH" includes a shell change and therefore more learning curve than "add a few lines to .bashrc".

This is getting off topic, pedantic, and (maybe?) argumentative so I'm calling it quits. Cheers.

BeetleB 3 days ago | parent [-]

I believe the default shell in MacOS is zsh, not bash. So lots of folks use zsh by default.

BeetleB 3 days ago | parent | prev | next [-]

It's (likely) simpler with zoxide + fzf.

I use autojump, which is a lot like zoxide (possible predates it). It stores all the directories you've visited in an SQLite DB (along with the rank for each). I wrote a shell keybinding that presents me with fzf, along with the directories I've visited, in rank order.

With just a few keystrokes, I can visit any directory I've ever visited, really fast. It doesn't need to be the top ranking directory for my query.

I can't live without it now.

robenkleene 3 days ago | parent | prev [-]

Yeah the problem with this approach to me is populating the history with a bunch of cd to absolute paths to begin with, which is not something I'd do natural (I have many ways I'm navigating the file system), and definitely wouldn't do manually. Not having to populate that list is the advantage of zoxide.

jvanderbot 3 days ago | parent | next [-]

From 2024 when this was last posted: It appears to me that these could all be just tab complete, fzf, or similar. Some are just plain old bash.

The commands are written in rust, presumably because they're part of this warp shell advertised on readme?

I used to use this https://github.com/jodavaho/smartcd

'scd journal'

'scd logs personal'

Now I tab complete using fzf, but the above is what you want.

Bash is Turing complete. You "need" nothing else. You may want it for various reasons.

robenkleene 3 days ago | parent [-]

- Recursive directory tab completion isn't viable for many key use cases (it'll hang with both monorepos and networked volumes). The only thing that makes zoxide viable is that it only matches against visited directories. (Similarly fzf doesn't have much of a relation here besides encouraging recursive fuzzy matching, which I tend to avoid because it doesn't scale to complex directory structures.)

- Zoxide is probably sponsored by Warp, I doubt they have a relation beyond that, I'm not sure but they don't have much of a synergy beyond both being written in Rust. (The main point of relevance here is Warp is venture funded.)

- Zoxide having a database outside of the shell is actually a huge advantage to me, because it makes it easy to access your database outside of the shell (e.g., Zoxide integration in Vim).

nickjj 3 days ago | parent | prev [-]

I think that's ok if you have absolute paths mixed in.

If I have some long path like ~/src/open-source/dotfiles or /home/nick/src/open-source/dotfiles it all works with fuzzy matching. You can hit CTRL+r and then search for "cd dotfiles" and it finds it. The ~ isn't necessary, you can also do "^cd dotfiles"` for a tighter list of matches for paths that are more ambigious with other non-cd commands.