vimcommand-linefzfvim-fzf

Is there a way to cancel fzf by pressing escape


I have alias vif='vim $(fzf) in my ~/.zprofile and it works great; however, it always opens vim window even if I click esc button because I didn't find the file I was looking for. How do I get it to not open vim if I press esc?


Solution

  • Use a shell function, that will allow you to check whether fzf exited with a non-zero status. (It exits with status 130 if interrupted with Esc or Ctrl+C.)

    function vif() {
        local fname
        fname=$(fzf) || return
        vim "$fname"
    }
    
    function fcd() {
        local dirname
        dirname=$(find -type d | fzf) || return
        cd "$dirname"
    }
    

    The || return part will break out early when the return code is non-zero, so vim will only be invoked after a successful fzf execution.