FZF - Search the target folder

1.2k Views Asked by At

I've been using fzf for a while now and finding it really great, however there is one thing I want it to do but I don't think I'm Googling the correct words.

I use it with neovim on a zsh terminal and what I want to do is for it to search only the target directory that I've typed out.

$ vi folder_name/ # Ctrl + T here

Currently, when I hit Ctrl+T it searches the current directory and not just the content of folder_name.

Installed via Plug and in .zshrc I have:

    source ~/.fzf.zsh
    export FZF_DEFAULT_COMMAND='ag --hidden --ignore .git -l -g ""'
    export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"

I tried removing the export lines as I thought it might be its default behavior but no dice.

How can I achieve this?

2

There are 2 best solutions below

0
On

I'm not so familiar with Linux, but ctrl-t seems to just insert the result on the command line. Compare with the completion functions which seems to generate a prefix from the buffer and pass that as a path to the find command.

Try using the completion functions or use them as a guide

0
On

I can't think of a built in way to do this, but we can achieve anything in the terminal!

The first thing that comes to mind is to create a function that will automate this and the make it quicker by adding a keybinding.

Using zsh you can set you own CTRL key bindings. I would recommend using CTRL - f for example. I don't think that should conflict with any other bindings. We can call our function using the bindkey command.

Put these items into your .zshrc to have them available.

# Set CTRL + f to call our function
bindkey -s '^f' 'FzfViDir^M'

function FzfViDir()
{
  # Use fzf to get into the right folder. The ls command sends folder names to fzf
  cd $(ls -d */ | fzf)

  # Now fzf will only search the directory you have chosen
  vi $(fzf)
}