From ae86cdf09a3cf4cf50ac86a337df23be10186d42 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 28 Mar 2014 01:39:20 +0900 Subject: [PATCH] Make CTRL-T use tmux split when possible --- README.md | 6 ++++- install | 65 +++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index e830c29..f2a2740 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,10 @@ The install script will setup the following key bindings. - `CTRL-R` - Paste the selected command from history into the command line - `ALT-C` - cd into the selected directory +If you're on a tmux session, `CTRL-T` will launch fzf in a new split-window. You +may disable this tmux integration by setting `FZF_TMUX` to 0, or change the +height of the window with `FZF_TMUX_HEIGHT`. + The source code can be found in `~/.fzf.bash` and in `~/.fzf.zsh`. Auto-completion @@ -310,7 +314,7 @@ If you have set up fzf for Vim, `:FZF` command will be added. Note that the environment variables `FZF_DEFAULT_COMMAND` and `FZF_DEFAULT_OPTS` also apply here. -If you're on a tmux session, `:FZF`, will launch fzf in a new split-window whose +If you're on a tmux session, `:FZF` will launch fzf in a new split-window whose height can be adjusted with `g:fzf_tmux_height` (default: 15). However, the bang version (`:FZF!`) will always start in fullscreen. diff --git a/install b/install index b91d7ac..a32d066 100755 --- a/install +++ b/install @@ -95,12 +95,12 @@ EOF if [ $key_bindings -eq 0 ]; then if [ $shell = bash ]; then - cat >> $src << "EOF" + cat >> $src << "EOFZF" # Key bindings # ------------ if [[ $- =~ i ]]; then -__fsel() { +read -r -d '' __fsel <<'EOF' find * -path '*/\.*' -prune \ -o -type f -print \ -o -type d -print \ @@ -108,6 +108,14 @@ __fsel() { printf '%q ' "$item" done echo +EOF + +__fsel() { + eval "$__fsel" +} + +__fsel_tmux() { + tmux split-window -l ${FZF_TMUX_HEIGHT:-15} "tmux send-keys -t $TMUX_PANE \"\$($__fsel)\"" } __fcd() { @@ -115,12 +123,19 @@ __fcd() { dir=$(find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m) && printf 'cd %q' "$dir" } +__use_tmux=0 +[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-30} -gt 15 ] && __use_tmux=1 + if [ -z "$(set -o | grep '^vi.*on')" ]; then # Required to refresh the prompt after fzf bind '"\er": redraw-current-line' # CTRL-T - Paste the selected file path into the command line - bind '"\C-t": " \C-u \C-a\C-k$(__fsel)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er"' + if [ $__use_tmux -eq 1 ]; then + bind '"\C-t": " \C-u \C-a\C-k$(__fsel_tmux)\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"' + else + bind '"\C-t": " \C-u \C-a\C-k$(__fsel)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er"' + fi # CTRL-R - Paste the selected command from history into the command line bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"' @@ -133,7 +148,11 @@ else # CTRL-T - Paste the selected file path into the command line # - FIXME: Selected items are attached to the end regardless of cursor position - bind '"\C-t": "\eddi$(__fsel)\C-x\C-e\e0P$a \C-x\C-r"' + if [ $__use_tmux -eq 1 ]; then + bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"' + else + bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r"' + fi # CTRL-R - Paste the selected command from history into the command line bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"' @@ -142,27 +161,35 @@ else bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"' fi +unset __use_tmux + fi -EOF +EOFZF else - cat >> $src << "EOF" + cat >> $src << "EOFZF" # Key bindings # ------------ # CTRL-T - Paste the selected file path(s) into the command line -fzf-file-widget() { - local FILES - local IFS=" -" - FILES=($( - find * -path '*/\.*' -prune \ +read -r -d '' __fsel <<'EOF' + find * -path '*/\.*' -prune \ -o -type f -print \ -o -type d -print \ - -o -type l -print 2> /dev/null | fzf -m)) - unset IFS - FILES=$FILES:q - LBUFFER="${LBUFFER%% #} $FILES" - zle redisplay -} + -o -type l -print 2> /dev/null | fzf -m | while read item; do + printf '%q ' "$item" + done + echo +EOF + +if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-30} -gt 15 ]; then + fzf-file-widget() { + tmux split-window -l ${FZF_TMUX_HEIGHT:-15} "tmux send-keys -t $TMUX_PANE \"\$($__fsel)\"" + } +else + fzf-file-widget() { + LBUFFER="${LBUFFER%% #}$(eval "$__fsel")" + zle redisplay + } +fi zle -N fzf-file-widget bindkey '^T' fzf-file-widget @@ -183,7 +210,7 @@ fzf-history-widget() { zle -N fzf-history-widget bindkey '^R' fzf-history-widget -EOF +EOFZF fi fi