Skip to content
Snippets Groups Projects
key-bindings.zsh 1.31 KiB
Newer Older
Junegunn Choi's avatar
Junegunn Choi committed
# Key bindings
# ------------
# CTRL-T - Paste the selected file path(s) into the command line
__fsel() {
  command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
    -o -type f -print \
    -o -type d -print \
    -o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf-tmux -d${FZF_TMUX_HEIGHT:-40%} -m | while read item; do
Junegunn Choi's avatar
Junegunn Choi committed
    printf '%q ' "$item"
  done
  echo
}

if [[ $- =~ i ]]; then

fzf-file-widget() {
  LBUFFER="${LBUFFER}$(__fsel)"
  zle redisplay
}
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() {
  cd "${$(command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
    -o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf-tmux -d${FZF_TMUX_HEIGHT:-40%} +m):-.}"
Junegunn Choi's avatar
Junegunn Choi committed
  zle reset-prompt
}
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() {
  local selected
  if selected=$(fc -l 1 | fzf-tmux -d${FZF_TMUX_HEIGHT:-40%} +s --tac +m -n2..,.. --tiebreak=index --toggle-sort=ctrl-r -q "$LBUFFER"); then
Junegunn Choi's avatar
Junegunn Choi committed
    num=$(echo "$selected" | head -1 | awk '{print $1}' | sed 's/[^0-9]//g')
    LBUFFER=!$num
    zle expand-history
  fi
  zle redisplay
}
zle     -N   fzf-history-widget
bindkey '^R' fzf-history-widget

fi