summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-03-06 18:51:50 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-03-06 18:51:50 +0900
commite4ce64d10b766e94a26e53b63199ee0dd66648b3 (patch)
tree2aabd0e4677d5fa97a5e7598104b2cb75e2ea19a /bin
parent5f3326a888f8a2afc3f163a705462c58fe591412 (diff)
Add fzf-tmux script
Diffstat (limited to 'bin')
-rwxr-xr-xbin/fzf-tmux64
1 files changed, 64 insertions, 0 deletions
diff --git a/bin/fzf-tmux b/bin/fzf-tmux
new file mode 100755
index 00000000..02d935ec
--- /dev/null
+++ b/bin/fzf-tmux
@@ -0,0 +1,64 @@
+#!/usr/bin/env bash
+# fzf-tmux: starts fzf in a tmux split
+# usage: fzf-tmux [-h HEIGHT[%]] [-w WIDTH[%]] [--] [FZF OPTIONS]
+
+args=()
+opt=""
+while [ $# -gt 0 ]; do
+ arg="$1"
+ case "$arg" in
+ -w*|-h*)
+ if [ ${#arg} -gt 2 ]; then
+ size="${arg:2}"
+ else
+ shift
+ size="$1"
+ fi
+ [[ "$arg" =~ ^-w ]] && opt="-h"
+ [[ "$size" =~ %$ ]] && opt="$opt -p ${size:0:-1}" ||
+ opt="$opt -l $size"
+ ;;
+ --)
+ # "--" can be used to separate fzf-tmux options from fzf options to
+ # avoid conflicts
+ break
+ ;;
+ *)
+ args+=("$1")
+ ;;
+ esac
+ shift
+done
+
+if [ -z "$TMUX_PANE" ]; then
+ fzf "${args[@]}"
+ exit $?
+fi
+
+set -e
+
+# Build arguments to fzf
+[ ${#args[@]} -gt 0 ] && fzf_args=$(printf '\\"%s\\" ' "${args[@]}"; echo '')
+
+# Clean up named pipes on exit
+id=$RANDOM
+fifo1=/tmp/fzf-fifo1-$id
+fifo2=/tmp/fzf-fifo2-$id
+fifo3=/tmp/fzf-fifo3-$id
+cleanup() {
+ rm -f $fifo1 $fifo2 $fifo3
+}
+trap cleanup EXIT SIGINT SIGTERM
+
+mkfifo $fifo2
+mkfifo $fifo3
+if [ -t 0 ]; then
+ tmux split-window $opt 'bash -c "fzf '"$fzf_args"' > '$fifo2'; echo \$? > '$fifo3'"'
+else
+ mkfifo $fifo1
+ tmux split-window $opt 'bash -c "fzf '"$fzf_args"' < '$fifo1' > '$fifo2'; echo \$? > '$fifo3'"'
+ cat <&0 > $fifo1 &
+fi
+cat $fifo2
+[ "$(cat $fifo3)" = '0' ]
+