Skip to content

Instantly share code, notes, and snippets.

@chew-z
Created February 19, 2017 16:25
Show Gist options
  • Save chew-z/078d8a68945bb26ca83e5b829c505bf0 to your computer and use it in GitHub Desktop.
Save chew-z/078d8a68945bb26ca83e5b829c505bf0 to your computer and use it in GitHub Desktop.
My CtrlP - using fzf - open, edit or reveal in finder
#!/usr/bin/env zsh
#
# https://adamheins.com/blog/ctrl-p-in-the-terminal-with-fzf
# This is the same functionality as fzf's ctrl-t, except that the file or
# directory selected is now automatically cd'ed or opened, respectively.
fzf-edit-file-or-open-dir() {
local out key file helpline
helpline="Ctrl-f to open in Finder | uses ag -g\"\" respecting .ignore"
IFS=$'\n' out=($(ag -g "" | fzf --header="$helpline" --exit-0 --expect=ctrl-f))
key=$(head -1 <<< "$out")
file=$(head -2 <<< "$out" | tail -1)
if [ "$key" = ctrl-f ]; then
open -R "$file" # reveal in Finder
else
if [ -f "$file" ]; then
open -a MacVim "$file"
elif [ -d "$file" ]; then
cd "$file"
fi
zle reset-prompt
fi
zle accept-line
}
zle -N fzf-edit-file-or-open-dir
bindkey '^P' fzf-edit-file-or-open-dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment