Merge pull request #30 from junegunn/keybinding-tmux-split

Make CTRL-T use tmux split when possible
This commit is contained in:
Junegunn Choi 2014-03-28 15:32:23 +09:00
commit 995d380200
2 changed files with 51 additions and 20 deletions

View File

@ -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 - `CTRL-R` - Paste the selected command from history into the command line
- `ALT-C` - cd into the selected directory - `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`. The source code can be found in `~/.fzf.bash` and in `~/.fzf.zsh`.
Auto-completion 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` Note that the environment variables `FZF_DEFAULT_COMMAND` and `FZF_DEFAULT_OPTS`
also apply here. 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 height can be adjusted with `g:fzf_tmux_height` (default: 15). However, the bang
version (`:FZF!`) will always start in fullscreen. version (`:FZF!`) will always start in fullscreen.

57
install
View File

@ -95,12 +95,12 @@ EOF
if [ $key_bindings -eq 0 ]; then if [ $key_bindings -eq 0 ]; then
if [ $shell = bash ]; then if [ $shell = bash ]; then
cat >> $src << "EOF" cat >> $src << "EOFZF"
# Key bindings # Key bindings
# ------------ # ------------
if [[ $- =~ i ]]; then if [[ $- =~ i ]]; then
__fsel() { read -r -d '' __fsel <<'EOF'
find * -path '*/\.*' -prune \ find * -path '*/\.*' -prune \
-o -type f -print \ -o -type f -print \
-o -type d -print \ -o -type d -print \
@ -108,6 +108,14 @@ __fsel() {
printf '%q ' "$item" printf '%q ' "$item"
done done
echo echo
EOF
__fsel() {
eval "$__fsel"
}
__fsel_tmux() {
tmux split-window -l ${FZF_TMUX_HEIGHT:-15} "tmux send-keys -t $TMUX_PANE \"\$($__fsel)\""
} }
__fcd() { __fcd() {
@ -115,12 +123,19 @@ __fcd() {
dir=$(find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m) && printf 'cd %q' "$dir" 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 if [ -z "$(set -o | grep '^vi.*on')" ]; then
# Required to refresh the prompt after fzf # Required to refresh the prompt after fzf
bind '"\er": redraw-current-line' bind '"\er": redraw-current-line'
# CTRL-T - Paste the selected file path into the command line # CTRL-T - Paste the selected file path into the command line
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"' 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 # 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"' 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 # CTRL-T - Paste the selected file path into the command line
# - FIXME: Selected items are attached to the end regardless of cursor position # - 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 # 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"' 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"' bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'
fi fi
unset __use_tmux
fi fi
EOF EOFZF
else else
cat >> $src << "EOF" cat >> $src << "EOFZF"
# Key bindings # Key bindings
# ------------ # ------------
# CTRL-T - Paste the selected file path(s) into the command line # CTRL-T - Paste the selected file path(s) into the command line
fzf-file-widget() { read -r -d '' __fsel <<'EOF'
local FILES
local IFS="
"
FILES=($(
find * -path '*/\.*' -prune \ find * -path '*/\.*' -prune \
-o -type f -print \ -o -type f -print \
-o -type d -print \ -o -type d -print \
-o -type l -print 2> /dev/null | fzf -m)) -o -type l -print 2> /dev/null | fzf -m | while read item; do
unset IFS printf '%q ' "$item"
FILES=$FILES:q done
LBUFFER="${LBUFFER%% #} $FILES" 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 zle redisplay
} }
fi
zle -N fzf-file-widget zle -N fzf-file-widget
bindkey '^T' fzf-file-widget bindkey '^T' fzf-file-widget
@ -183,7 +210,7 @@ fzf-history-widget() {
zle -N fzf-history-widget zle -N fzf-history-widget
bindkey '^R' fzf-history-widget bindkey '^R' fzf-history-widget
EOF EOFZF
fi fi
fi fi