0

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?

user4426017
  • 1,930
  • 17
  • 31

1 Answers1

2

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.

SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
filbranden
  • 8,522
  • 2
  • 16
  • 32