Skip to content
Snippets Groups Projects
key-bindings.zsh 2.45 KiB
Newer Older
Junegunn Choi's avatar
Junegunn Choi committed
# Key bindings
# ------------
if [[ $- == *i* ]]; then
Junegunn Choi's avatar
Junegunn Choi committed
# CTRL-T - Paste the selected file path(s) into the command line
__fsel() {
  local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
Junegunn Choi's avatar
Junegunn Choi committed
    -o -type f -print \
    -o -type d -print \
    -o -type l -print 2> /dev/null | cut -b3-"}"
  setopt localoptions pipefail 2> /dev/null
  eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@" | while read item; do
    echo -n "${(q)item} "
Junegunn Choi's avatar
Junegunn Choi committed
  done
Junegunn Choi's avatar
Junegunn Choi committed
  echo
Junegunn Choi's avatar
Junegunn Choi committed
__fzf_use_tmux__() {
  [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]
}

Junegunn Choi's avatar
Junegunn Choi committed
  __fzf_use_tmux__ &&
    echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"
fzf-file-widget() {
  LBUFFER="${LBUFFER}$(__fsel)"
  zle redisplay
  typeset -f zle-line-init >/dev/null && zle zle-line-init
Junegunn Choi's avatar
Junegunn Choi committed
zle     -N   fzf-file-widget
bindkey '^T' fzf-file-widget

# ALT-C - cd into the selected directory
fzf-cd-widget() {
  local cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
    -o -type d -print 2> /dev/null | cut -b3-"}"
  setopt localoptions pipefail 2> /dev/null
  local dir="$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m)"
  if [[ -z "$dir" ]]; then
    zle redisplay
    return 0
  fi
  cd "$dir"
Junegunn Choi's avatar
Junegunn Choi committed
  zle reset-prompt
  typeset -f zle-line-init >/dev/null && zle zle-line-init
Junegunn Choi's avatar
Junegunn Choi committed
}
zle     -N    fzf-cd-widget
bindkey '\ec' fzf-cd-widget

# CTRL-R - Paste the selected command from history into the command line
fzf-history-widget() {
  setopt localoptions noglobsubst noposixbuiltins pipefail 2> /dev/null
    FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS --tac -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
  if [ -n "$selected" ]; then
      zle vi-fetch-history -n $num
Junegunn Choi's avatar
Junegunn Choi committed
  fi
  zle redisplay
  typeset -f zle-line-init >/dev/null && zle zle-line-init
Junegunn Choi's avatar
Junegunn Choi committed
}
zle     -N   fzf-history-widget
bindkey '^R' fzf-history-widget

fi