13

When I use :Files in fzf.vim, it searches for the files in the current directory and the subdirectories. The current working directory is fixed. In the screenshot below, it is in light blue. Is there a way to dynamically change the working directory on which FZF is run? For example, if I just delete "Documents" path, then FZF starts on ~ instead of ~/Documents.

Such functionality is possible in Emacs helm package, which allows this through helm-execute-persistent-action.

enter image description here

zcadqe
  • 937
  • 2
  • 13
  • 20
  • Looks like this is now possible with the reload action in fzf; see https://stackoverflow.com/a/72946706/2144408 and https://github.com/junegunn/fzf/issues/2872 – TamaMcGlinn Jul 06 '23 at 13:27

2 Answers2

10

You could change the current working directory vim is currently using the :chdir or the familiar :cd command. See :h cd for more info.

And if you don't want to change the working directory of vim and just want fzf to use a different directory, you could run fzf (:h fzf#run) with a custom dir option.

This is an example mapping I have in my ~/.vimrc to open a file within my /.vim directory:

nnoremap <leader>fv :call fzf#run({'options': '--reverse --prompt "VimFiles"', 'down': 20, 'dir': '~/.vim/', 'sink': 'e' })<CR>

For dynamic path you add a command that takes the path as an argument, make sure to use the completion as file for tab completing the path. see this answer for more info on file completion.

The sink option tells what to do/perform when a match is found. and e is for editing the main window.

Harish
  • 1,433
  • 9
  • 21
  • 2
    I may have misunderstood, but isn't the question asking how to change the directory while the `fzf` window is still running? I.e., being able to hit `backspace` (or `../`) without exiting the `fzf` window/starting over. – SO_fix_the_vote_sorting_bug Aug 13 '20 at 18:20
  • If @jdk1.0 is right, I read it's not possible. I can't find the source again but I stumbled on it looking for the same thing. – Adrien Sep 29 '20 at 10:45
0

It is not possible. Meanwhile there is a workaround proposed by the author. Take a look here:

If you understand what fzf really is and how it works (it's a generic, context-free text filter running outside of Vim), you'll realize that it's not something we can easily implement with fzf. But you can experiment with --expect option or reload binding. I think this is the best we can do for the moment:

" Reloading source on CTRL-P. Requires fd command.
function! Foo(dir)
  let tf = tempname()
  call writefile(['.'], tf)

  call fzf#vim#files(a:dir, {'source': 'fd', 'options': ['--bind', printf('ctrl-p:reload:base="$(cat %s)"/..; echo "$base" > %s; fd . "$base"', shellescape(tf), shellescape(tf))]})
endfunction

command! -nargs=* Foo call Foo(<q-args>)

https://github.com/junegunn/fzf.vim/issues/338#issuecomment-623087034

Adrien
  • 2,088
  • 1
  • 18
  • 35