fre for files : how?
Opened this issue · 10 comments
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.
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
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'
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.
I have something working - do you want me to contribute something to the docs about it?
Hey! Glad to hear you got it working. Adding something to the docs would be great -- thank you!
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
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.
Is this enough for you to put something under examples?
Yes! This is great -- I'll get this integrated. Thanks!
I saw the other discussion about paths, and now I have fre --add "$(realpath $cand_fname)"