Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fre for files : how? #25

Open
jefftemplon opened this issue Jan 16, 2024 · 10 comments
Open

fre for files : how? #25

jefftemplon opened this issue Jan 16, 2024 · 10 comments

Comments

@jefftemplon
Copy link

Hi,

The intro in the docs says "for tracking your most-used directories and files" - everything else in the docs refers to directories. How to use it for files?

What I'd like to achieve is to replace fasd, so when I type less ,2022, I get a completion list of all files and directories I've recently accessed that have 2022 somewhere in the name or path, frecency sorted.

@camdencheek
Copy link
Owner

Hey @jefftemplon! fre just provides an API to store access events -- it does not mandate where those events come from. Directories are fairly straightforward because we can add shell hooks that log an access event with fre --add. Files are a little trickier because "opening a file" can mean a bunch of different things:

  • Open in vim
  • Open in emacs
  • Inspect with cat
  • Rename the file
  • echo "test" >> file
  • ...plus basically any other thing that interacts with a file

So, to get it to work with files, you'll need to define which events count as an access, then add a hook to call fre --add <file>. You might decide that you really only care about files you open with vim, so you add an autocommand that calls fre --add on the file. I don't personally use fre for files, so I don't have any complete examples

@jefftemplon
Copy link
Author

jefftemplon commented Feb 5, 2024 via email

@jefftemplon
Copy link
Author

I've got something working! A related question: can fre be built statically, so that a binary I produce can run on other machines that don't have any rust libraries installed?

What works:

preexec () {
#   echo $1 $2
   local tmpcmd="$1"
   local cand_fname="${tmpcmd##* }"
#   echo "cand_fname $cand_fname"
   [ -f "$cand_fname" ] && fre --add "$cand_fname"
}

export FZF_CTRL_T_COMMAND='command cat <(fre --sorted) <(fd -t f -t d) <(fd -t d . ~)'
export FZF_CTRL_T_OPTS='--tiebreak=index'

@jefftemplon
Copy link
Author

Hi, managed to deal with the fre binary. The only thing I have left is that the expansion for cand_fname doesn't work when the filename has embedded spaces. That's a shell thing, not a fre thing.

@jefftemplon
Copy link
Author

I have something working - do you want me to contribute something to the docs about it?

@camdencheek
Copy link
Owner

Hey! Glad to hear you got it working. Adding something to the docs would be great -- thank you!

@jefftemplon
Copy link
Author

jefftemplon commented Feb 9, 2024

This note documents how to use fre together with fzf to find files instead of directories.

The use case

I often access files located in /var or other locations outside of my own home-directory hierarchy. I used to use fasd for this, however this project is unsupported. Given the setup described below, files I access are passed to fre and are then available in the fzf listing when I do e.g. ctrl-t

See attached screenshot
Screenshot 2024-02-09 at 15 14 00

Here I had typed less (note the trailing space) and then ctrl-t - that list of files is the result. I can scroll via arrow keys, or type more characters to limit the search results (see the fzf docs for this).

Here is an example zsh (via oh-my-zsh) configuration that enables this:

plugins=(zsh-autosuggestions fzf)

fre_preexec () {
   local tmpcmd="$3"
   local cand_fname=${(Q)${(z)tmpcmd}[-1]}
   [ -f "$cand_fname" ] && fre --add "$cand_fname"
}
add-zsh-hook preexec fre_preexec

export FZF_DEFAULT_COMMAND='fd --type f --strip-cwd-prefix'
export FZF_CTRL_T_COMMAND='command cat <(fre --sorted) <(fd -t f -t d) <(fd -t d . ~)'
export FZF_CTRL_T_OPTS='--tiebreak=index'

fre_preexec examines each executed command and checks whether the last argument of that command is a valid file name, if so it's added to the fre database. The fzf environment variables shown assume that fd is installed.

@jefftemplon
Copy link
Author

Is this enough for you to put something under examples?

@camdencheek
Copy link
Owner

Yes! This is great -- I'll get this integrated. Thanks!

@jefftemplon
Copy link
Author

I saw the other discussion about paths, and now I have fre --add "$(realpath $cand_fname)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants