bashdirectoryfilesystemsfzfvim-fzf

How to start fzf from a different directory than the current working directory?


I often use fzf to navigate the filesystem, especially the Alt-c key binding.

When invoked, fzf generates a list from the current working directory.

Is it possible to make fzf generate a list from a specified directory?

I have tried fzf <dir>, but it results in an error (unknown option). Also, I can't find any options like -C <dir> for specifying the start directory.


Solution

  • Assuming you're using bash or similar, this is built into the default completion options which get installed with fzf: https://github.com/junegunn/fzf#fuzzy-completion-for-bash-and-zsh

    tldr; enter start file or directory, append ** and hit Tab. So if you'd enter cd /foo/** then tab opens fzf with /foo as start directory.

    edit at the time of writing the commands for which this works are hardcoded in fzf's bash helpers, which is why this works for cat and cd but not for tac or nano. This is the complete list:

    awk cat diff diff3
    emacs emacsclient ex file ftp g++ gcc gvim head hg java
    javac ld less more mvim nvim patch perl python ruby
    sed sftp sort source tail tee uniq vi view vim wc xdg-open
    basename bunzip2 bzip2 chmod chown curl cp dirname du
    find git grep gunzip gzip hg jar
    ln ls mv open rm rsync scp
    svn tar unzip zip
    

    To add other commands use this in e.g. your .bashrc, after the place where fzf gets sourced (something like [ -f ~/.fzf.bash ] && source ~/.fzf.bash):

    __fzf_defc "tac" _fzf_path_completion "-o default -o bashdefault"
    

    Alternatively: open an issue to ask for the commands you want to be added to fzf by default, things like tac and nano are super common anyway.