Merge pull request #138 from junegunn/fzf-tmux-swap-pane

[fzf-tmux] Allow opening fzf on any position (up/down/left/right)
This commit is contained in:
Junegunn Choi 2015-03-09 23:28:53 +09:00
commit e4b56b9702
2 changed files with 48 additions and 14 deletions

View File

@ -152,17 +152,17 @@ installer-generated source code: `~/.fzf.bash`, `~/.fzf.zsh`, and
[fzf-tmux](bin/fzf-tmux) is a bash script that opens fzf in a tmux pane. [fzf-tmux](bin/fzf-tmux) is a bash script that opens fzf in a tmux pane.
```sh ```sh
# usage: fzf-tmux [-h [HEIGHT[%]]] [-w [WIDTH[%]]] [--] [FZF OPTIONS] # usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
# select git branches in horizontal split (15 lines) # select git branches in horizontal split below (15 lines)
git branch | fzf-tmux -h 15 git branch | fzf-tmux -d 15
# select multiple words in vertical split (20% of screen width) # select multiple words in vertical split on the left (20% of screen width)
cat /usr/share/dict/words | fzf-tmux -w 20% --multi cat /usr/share/dict/words | fzf-tmux -l 20% --multi --reverse
``` ```
It will still work even when you're not on tmux, silently ignoring `-h` and It will still work even when you're not on tmux, silently ignoring `-[udlr]`
`-w` options, so you can invariably use `fzf-tmux` in your scripts. options, so you can invariably use `fzf-tmux` in your scripts.
Fuzzy completion for bash Fuzzy completion for bash
------------------------- -------------------------

View File

@ -1,20 +1,36 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# fzf-tmux: starts fzf in a tmux pane # fzf-tmux: starts fzf in a tmux pane
# usage: fzf-tmux [-h [HEIGHT[%]]] [-w [WIDTH[%]]] [--] [FZF OPTIONS] # usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
args=() args=()
opt="" opt=""
skip="" skip=""
swap=""
close=""
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
arg="$1" arg="$1"
case "$arg" in case "$arg" in
-w*|-h*) -w*|-h*|-d*|-u*|-r*|-l*)
if [ -n "$skip" ]; then if [ -n "$skip" ]; then
args+=("$1") args+=("$1")
shift shift
continue continue
fi fi
[[ "$arg" =~ ^-w ]] && opt="-h" || opt="" if [[ "$arg" =~ ^.[lrw] ]]; then
opt="-h"
if [[ "$arg" =~ ^.l ]]; then
opt="$opt -d"
swap="; swap-pane -D ; select-pane -L"
close="; tmux swap-pane -D"
fi
else
opt=""
if [[ "$arg" =~ ^.u ]]; then
opt="$opt -d"
swap="; swap-pane -D ; select-pane -U"
close="; tmux swap-pane -D"
fi
fi
if [ ${#arg} -gt 2 ]; then if [ ${#arg} -gt 2 ]; then
size="${arg:2}" size="${arg:2}"
else else
@ -27,8 +43,24 @@ while [ $# -gt 0 ]; do
continue continue
fi fi
fi fi
[[ "$size" =~ %$ ]] && opt="$opt -p ${size:0:((${#size}-1))}" ||
opt="$opt -l $size" if [[ "$size" =~ %$ ]]; then
size=${size:0:((${#size}-1))}
if [ -n "$swap" ]; then
opt="$opt -p $(( 100 - size ))"
else
opt="$opt -p $size"
fi
else
if [ -n "$swap" ]; then
[[ "$arg" =~ ^.l ]] && max=$(tput cols) || max=$(tput lines)
size=$(( max - size ))
[ $size -lt 0 ] && size=0
opt="$opt -l $size"
else
opt="$opt -l $size"
fi
fi
;; ;;
--) --)
# "--" can be used to separate fzf-tmux options from fzf options to # "--" can be used to separate fzf-tmux options from fzf options to
@ -71,10 +103,12 @@ fzf=$(which fzf 2> /dev/null) || fail "fzf executable not found"
mkfifo $fifo2 mkfifo $fifo2
mkfifo $fifo3 mkfifo $fifo3
if [ -t 0 ]; then if [ -t 0 ]; then
tmux split-window $opt 'sh -c "'$fzf' '"$fzf_args"' > '$fifo2'; echo \$? > '$fifo3'"' tmux set-window-option -q synchronize-panes off \;\
split-window $opt 'sh -c "'$fzf' '"$fzf_args"' > '$fifo2'; echo \$? > '$fifo3' '"$close"'"' $swap
else else
mkfifo $fifo1 mkfifo $fifo1
tmux split-window $opt 'sh -c "'$fzf' '"$fzf_args"' < '$fifo1' > '$fifo2'; echo \$? > '$fifo3'"' tmux set-window-option -q synchronize-panes off \;\
split-window $opt 'sh -c "'$fzf' '"$fzf_args"' < '$fifo1' > '$fifo2'; echo \$? > '$fifo3' '"$close"'"' $swap
cat <&0 > $fifo1 & cat <&0 > $fifo1 &
fi fi
cat $fifo2 cat $fifo2