[fzf-tmux] Add option to start fzf in tmux popup window
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Requires latest tmux built from source (e.g. brew install tmux --HEAD) Examples: # 50%/50% width and height on the center of the screen fzf-tmux -p # 80%/80% fzf-tmux -p80% # 80%/40% fzf-tmux -p80%,40% # Separate -w and -h fzf-tmux -w80% -h40% # 80%/40% at position (0, 0) fzf-tmux -w80% -h40% -x0 -y0 You can configure key bindings and fuzzy completion to open in tmux popup window like so: FZF_TMUX_OPTS='-p 80%'
authored by Junegunn Choi · committed by Junegunn Choi · parent 0f5c6e8 · Browse files · 9 files·+153 added−92 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+153 added−92 removed
- tests:+13 added−15 removed
- other:+140 added−77 removed
Provenance
- Commit
bf65e8c- Tree
b2717ff- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
0f5c6e8
9 files changed+153 added−92 removed
modified:bin/fzf-tmux+50 added-16 removed
1 #!/usr/bin/env bash2 # fzf-tmux: starts fzf in a tmux pane3-# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]3+# usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]4 5 fail() {6 >&2 echo "$1"7 exit 21 unmodified line9 10 fzf="$(command -v fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf"11 [[ -x "$fzf" ]] || fail 'fzf executable not found'12 13+tmux_args=()14 args=()15 opt=""16 skip=""17 swap=""2 unmodified lines20 [[ -n "$LINES" ]] && lines=$LINES || lines=$(tput lines) || lines=$(tmux display-message -p "#{pane_height}")21 [[ -n "$COLUMNS" ]] && columns=$COLUMNS || columns=$(tput cols) || columns=$(tmux display-message -p "#{pane_width}")22 23 help() {23- >&2 echo 'usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]24+ >&2 echo 'usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]25 25- Layout26- -u [HEIGHT[%]] Split above (up)27- -d [HEIGHT[%]] Split below (down)28- -l [WIDTH[%]] Split left29- -r [WIDTH[%]] Split right26+ LAYOUT OPTIONS:27+ (default layout: -d 50%)28 31- (default: -d 50%)29+ Popup window (requires tmux 3.2 or above):30+ -p [WIDTH[%][,HEIGHT[%]]] (default: 50%)31+ -w WIDTH[%]32+ -h HEIGHT[%]33+ -x COL34+ -y ROW35+36+ Split pane:37+ -u [HEIGHT[%]] Split above (up)38+ -d [HEIGHT[%]] Split below (down)39+ -l [WIDTH[%]] Split left40+ -r [WIDTH[%]] Split right41 '42 exit43 }44 10 unmodified lines55 --version)56 echo "fzf-tmux (with fzf $("$fzf" --version))"57 exit58 ;;50- -w*|-h*|-d*|-u*|-r*|-l*)51- if [[ "$arg" =~ ^.[lrw] ]]; then59+ -p*|-w*|-h*|-x*|-y*|-d*|-u*|-r*|-l*)60+ if [[ "$arg" =~ ^-[pwhxy] ]]; then61+ [[ "$opt" =~ "-K -E" ]] || opt="-K -E"62+ elif [[ "$arg" =~ ^.[lr] ]]; then63 opt="-h"64 if [[ "$arg" =~ ^.l ]]; then65 opt="$opt -d"66 swap="; swap-pane -D ; select-pane -L"9 unmodified lines76 fi77 if [[ ${#arg} -gt 2 ]]; then78 size="${arg:2}"79 else69- if [[ "$1" =~ ^[0-9]+%?$ ]]; then80+ if [[ "$1" =~ ^[0-9%,C]+$ ]]; then81 size="$1"82 shift83 else84 continue85 fi86 fi87 77- if [[ "$size" =~ %$ ]]; then88+ if [[ "$arg" =~ ^-p ]]; then89+ if [[ -n "$size" ]]; then90+ w=${size%%,*}91+ h=${size##*,}92+ opt="$opt -w$w -h$h"93+ fi94+ elif [[ "$arg" =~ ^-[whxy] ]]; then95+ opt="$opt ${arg:0:2}$size"96+ elif [[ "$size" =~ %$ ]]; then97 size=${size:0:((${#size}-1))}98 if [[ -n "$swap" ]]; then99 opt="$opt -p $(( 100 - size ))"100 else17 unmodified lines118 --)119 # "--" can be used to separate fzf-tmux options from fzf options to120 # avoid conflicts121 skip=1122+ tmux_args=("${args[@]}")123+ args=()124 continue125 ;;126 *)127 args+=("$arg")7 unmodified lines135 exit $?136 fi137 138 # --height option is not allowed118-args+=("--no-height")139+args=("--no-height" "${args[@]}")140 141 # Handle zoomed tmux pane by moving it to a temp window142 if tmux list-panes -F '#F' | grep -q Z; then143 zoomed=157 unmodified lines201 close="; trap - EXIT SIGINT SIGTERM $close"202 203 tmux_win_opts=( $(tmux show-window-options remain-on-exit \; show-window-options synchronize-panes | sed '/ off/d; s/^/set-window-option /; s/$/ \\;/') )204 205+if [[ "$opt" =~ "-K -E" ]]; then206+ cat $fifo2 &207+ if [[ -n "$term" ]] || [[ -t 0 ]]; then208+ cat <<< "\"$fzf\" $opts > $fifo2; out=\$? $close; exit \$out" >> $argsf209+ TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux popup -d '#{pane_current_path}' "${tmux_args[@]}" $opt -R "$envs bash $argsf" > /dev/null 2>&1210+ else211+ mkfifo $fifo1212+ cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; out=\$? $close; exit \$out" >> $argsf213+ cat <&0 > $fifo1 &214+ TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux popup -d '#{pane_current_path}' "${tmux_args[@]}" $opt -R "$envs bash $argsf" > /dev/null 2>&1215+ fi216+ exit $?217+fi218+219 if [[ -n "$term" ]] || [[ -t 0 ]]; then220 cat <<< "\"$fzf\" $opts > $fifo2; echo \$? > $fifo3 $close" >> $argsf221 TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux set-window-option synchronize-panes off \;\222 set-window-option remain-on-exit off \;\188- split-window $opt "$envs bash -c 'cd $(printf %q "$PWD"); exec -a fzf bash $argsf'" $swap \223+ split-window $opt "${tmux_args[@]}" "$envs bash -c 'cd $(printf %q "$PWD"); exec -a fzf bash $argsf'" $swap \224 > /dev/null 2>&1225 else226 mkfifo $fifo1227 cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" >> $argsf228 TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux set-window-option synchronize-panes off \;\229 set-window-option remain-on-exit off \;\195- split-window $opt "$envs bash -c 'exec -a fzf bash $argsf'" $swap \230+ split-window $opt "${tmux_args[@]}" "$envs bash -c 'exec -a fzf bash $argsf'" $swap \231 > /dev/null 2>&1232 cat <&0 > $fifo1 &233 fi234 cat $fifo2235 exit "$(cat $fifo3)"201-modified:man/man1/fzf-tmux.1+22 added-8 removed
25 unmodified lines26 .SH NAME27 fzf-tmux - open fzf in tmux split pane28 29 .SH SYNOPSIS30-.B fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]30+.B fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]31 32 .SH DESCRIPTION33-fzf-tmux is a wrapper script for fzf that opens fzf in a tmux split pane. It is34-designed to work just like fzf except that it does not take up the whole35-screen. You can safely use fzf-tmux instead of fzf in your scripts as the extra36-options will be silently ignored if you're not on tmux.33+fzf-tmux is a wrapper script for fzf that opens fzf in a tmux split pane or in34+a tmux popup window. It is designed to work just like fzf except that it does35+not take up the whole screen. You can safely use fzf-tmux instead of fzf in36+your scripts as the extra options will be silently ignored if you're not on37+tmux.38 38-.SH OPTIONS39-.SS Layout39+.SH LAYOUT OPTIONS40 41-(default: \fB-d 50%\fR)41+(default layout: \fB-d 50%\fR)42 43+.SS Popup window44+(requires tmux 3.2 or above)45 .TP46+.B "-p [WIDTH[%][,HEIGHT[%]]]"47+.TP48+.B "-w WIDTH[%]"49+.TP50+.B "-h WIDTH[%]"51+.TP52+.B "-x COL"53+.TP54+.B "-y ROW"55+56+.SS Split pane57+.TP58 .B "-u [height[%]]"59 Split above (up)60 .TP61 .B "-d [height[%]]"7 unmodified linesmodified:shell/completion.bash+4 added-4 removed
1 # ____ ____2 # / __/___ / __/3 # / /_/_ / / /_4 # / __/ / /_/ __/5-# /_/ /___/_/-completion.bash5+# /_/ /___/_/ completion.bash6 #7 # - $FZF_TMUX (default: 0)8-# - $FZF_TMUX_HEIGHT (default: '40%')8+# - $FZF_TMUX_OPTS (default: empty)9 # - $FZF_COMPLETION_TRIGGER (default: '**')10 # - $FZF_COMPLETION_OPTS (default: empty)11 12 if [[ $- =~ i ]]; then23 unmodified lines36 37 __fzf_comprun() {38 if [ "$(type -t _fzf_comprun 2>&1)" = function ]; then39 _fzf_comprun "$@"40- elif [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]; then40+ elif [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] || [ -n "$FZF_TMUX_OPTS" ]; }; then41 shift42- fzf-tmux -d "${FZF_TMUX_HEIGHT:-40%}" "$@"42+ fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- "$@"43 else44 shift45 fzf "$@"46 fi319 unmodified linesmodified:shell/completion.zsh+4 added-4 removed
1 # ____ ____2 # / __/___ / __/3 # / /_/_ / / /_4 # / __/ / /_/ __/5-# /_/ /___/_/-completion.zsh5+# /_/ /___/_/ completion.zsh6 #7 # - $FZF_TMUX (default: 0)8-# - $FZF_TMUX_HEIGHT (default: '40%')8+# - $FZF_TMUX_OPTS (default: '-d 40%')9 # - $FZF_COMPLETION_TRIGGER (default: '**')10 # - $FZF_COMPLETION_OPTS (default: empty)11 12 # Both branches of the following `if` do the same thing -- define85 unmodified lines98 99 __fzf_comprun() {100 if [[ "$(type _fzf_comprun 2>&1)" =~ function ]]; then101 _fzf_comprun "$@"102- elif [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]; then102+ elif [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] || [ -n "$FZF_TMUX_OPTS" ]; }; then103 shift104- fzf-tmux -d "${FZF_TMUX_HEIGHT:-40%}" "$@"104+ fzf-tmux ${(Q)${(Z+n+)FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}}} -- "$@"105 else106 shift107 fzf "$@"108 fi205 unmodified linesmodified:shell/key-bindings.bash+20 added-31 removed
123456789101112131415161718192082122232425262728161718192921223031323325262728293031323334353634383940414243353637443839404125 unmodified lines676869707879808171827273747521 unmodified lines# ____ ____# / __/___ / __/# / /_/_ / / /_# / __/ / /_/ __/# /_/ /___/_/ key-bindings.bash## - $FZF_TMUX_OPTS# - $FZF_CTRL_T_COMMAND# - $FZF_CTRL_T_OPTS# - $FZF_CTRL_R_OPTS# - $FZF_ALT_C_COMMAND# - $FZF_ALT_C_OPTS
# Key bindings# ------------__fzf_select__() { local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \ -o -type f -print \ -o -type d -print \ -o -type l -print 2> /dev/null | cut -b3-"}" eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" fzf -m "$@" | while read -r item; do eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@" | while read -r item; do printf '%q ' "$item" done echo}
if [[ $- =~ i ]]; then
__fzf_use_tmux__() { [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]}
__fzfcmd() { __fzf_use_tmux__ && echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf" [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] || [ -n "$FZF_TMUX_OPTS" ]; } && echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"}
__fzf_select_tmux__() { local height height=${FZF_TMUX_HEIGHT:-40%} if [[ $height =~ %$ ]]; then height="-p ${height%\%}" else height="-l $height" fi
tmux split-window $height "cd $(printf %q "$PWD"); FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS") PATH=$(printf %q "$PATH") FZF_CTRL_T_COMMAND=$(printf %q "$FZF_CTRL_T_COMMAND") FZF_CTRL_T_OPTS=$(printf %q "$FZF_CTRL_T_OPTS") bash -c 'source \"${BASH_SOURCE[0]}\"; RESULT=\"\$(__fzf_select__ --no-height)\"; tmux setb -b fzf \"\$RESULT\" \\; pasteb -b fzf -t $TMUX_PANE \\; deleteb -b fzf || tmux send-keys -t $TMUX_PANE \"\$RESULT\"'"}
fzf-file-widget() { if __fzf_use_tmux__; then __fzf_select_tmux__ else local selected="$(__fzf_select__)" READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}" READLINE_POINT=$(( READLINE_POINT + ${#selected} )) local selected="$(__fzf_select__)" READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}" READLINE_POINT=$(( READLINE_POINT + ${#selected} )) fi}
__fzf_cd__() { local cmd dir25 unmodified linesbind -m emacs-standard '"\C-z": vi-editing-mode'
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then # CTRL-T - Paste the selected file path into the command line if __fzf_use_tmux__; then bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select_tmux__`\e\C-e\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"' else bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select__`\e\C-e\er\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"' bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select__`\e\C-e\er\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"' fi bind -m vi-command '"\C-t": "\C-z\C-t\C-z"' bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"'
# CTRL-R - Paste the selected command from history into the command line21 unmodified linesmodified:shell/key-bindings.fish+17 added-2 removed
1+# ____ ____2+# / __/___ / __/3+# / /_/_ / / /_4+# / __/ / /_/ __/5+# /_/ /___/_/ key-bindings.fish6+#7+# - $FZF_TMUX_OPTS8+# - $FZF_CTRL_T_COMMAND9+# - $FZF_CTRL_T_OPTS10+# - $FZF_CTRL_R_OPTS11+# - $FZF_ALT_C_COMMAND12+# - $FZF_ALT_C_OPTS13+14 # Key bindings15 # ------------16 function fzf_key_bindings17 78 unmodified lines96 97 function __fzfcmd98 test -n "$FZF_TMUX"; or set FZF_TMUX 099 test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%100+ if [ -n "$FZF_TMUX_OPTS" ]101+ echo "fzf-tmux $FZF_TMUX_OPTS -- "87- if [ $FZF_TMUX -eq 1 ]88- echo "fzf-tmux -d$FZF_TMUX_HEIGHT"102+ else if [ $FZF_TMUX -eq 1 ]103+ echo "fzf-tmux -d$FZF_TMUX_HEIGHT -- "104 else105 echo "fzf"106 end107 end54 unmodified linesmodified:shell/key-bindings.zsh+15 added-6 removed
1+# ____ ____2+# / __/___ / __/3+# / /_/_ / / /_4+# / __/ / /_/ __/5+# /_/ /___/_/ key-bindings.zsh6+#7+# - $FZF_TMUX_OPTS8+# - $FZF_CTRL_T_COMMAND9+# - $FZF_CTRL_T_OPTS10+# - $FZF_CTRL_R_OPTS11+# - $FZF_ALT_C_COMMAND12+# - $FZF_ALT_C_OPTS13+14 # Key bindings15 # ------------16 17 # The code at the top and the bottom of this file is the same as in completion.zsh.34 unmodified lines52 echo53 return $ret54 }55 43-__fzf_use_tmux__() {44- [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]45-}46-56 __fzfcmd() {48- __fzf_use_tmux__ &&49- echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"57+ [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] || [ -n "$FZF_TMUX_OPTS" ]; } &&58+ echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"59 }60 61 fzf-file-widget() {62 LBUFFER="${LBUFFER}$(__fsel)"59 unmodified linesmodified:test/test_go.rb+13 added-15 removed
97 unmodified lines98 def kill99 go(%W[kill-window -t #{win}])100 end101 102+ def focus103+ go(%W[select-window -t #{win}])104+ end105+106 def send_keys(*args)103- target =104- if args.last.is_a?(Hash)105- hash = args.pop106- go(%W[select-window -t #{win}])107- "#{win}.#{hash[:pane]}"108- else109- win110- end111- go(%W[send-keys -t #{target}] + args.map(&:to_s))107+ go(%W[send-keys -t #{win}] + args.map(&:to_s))108 end109 110 def paste(str)111 system('tmux', 'setb', str, ';', 'pasteb', '-t', win, ';', 'send-keys', '-t', win, 'Enter')112 end113 118- def capture(pane = 0)119- go(%W[capture-pane -p -t #{win}.#{pane}]).reverse.drop_while(&:empty?).reverse114+ def capture115+ go(%W[capture-pane -p -t #{win}]).reverse.drop_while(&:empty?).reverse116 end117 122- def until(refresh = false, pane = 0)118+ def until(refresh = false)119 lines = nil120 begin121 wait do126- lines = capture(pane)122+ lines = capture123 class << lines124 def counts125 lazy126 .map { |l| l.scan %r{^. ([0-9]+)\/([0-9]+)( \(([0-9]+)\))?} }1908 unmodified lines2035 tmux.until { |lines| lines[-1].include? 'unset FZFFOOBAR' }2036 tmux.send_keys 'C-c'2037 2038 # FZF_TMUX=12039+ skip 'screen size too small' if `tput lines`.to_i < 152040 new_shell2041+ tmux.focus2044- tmux.send_keys 'unset FZFFOOBR**', :Tab, pane: 02045- tmux.until(false, 1) { |lines| lines.match_count == 1 }2042+ tmux.send_keys 'unset FZFFOOBR**', :Tab2043+ tmux.until { |lines| lines.match_count == 1 }2044 tmux.send_keys :Enter2045 tmux.until { |lines| lines[-1].include? 'unset FZFFOOBAR' }2046 end2047 167 unmodified linesmodified:README.md+8 added-6 removed
279 unmodified lines280 281 [fzf-tmux](bin/fzf-tmux) is a bash script that opens fzf in a tmux pane.282 283 ```sh284-# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]285-# (-[udlr]: up/down/left/right)284+# usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]285 286+# See available options287+fzf-tmux --help288+289 # select git branches in horizontal split below (15 lines)290 git branch | fzf-tmux -d 15291 292 # select multiple words in vertical split on the left (20% of screen width)293 cat /usr/share/dict/words | fzf-tmux -l 20% --multi --reverse294 ```295 294-It will still work even when you're not on tmux, silently ignoring `-[udlr]`296+It will still work even when you're not on tmux, silently ignoring `-[pudlr]`297 options, so you can invariably use `fzf-tmux` in your scripts.298 299 Alternatively, you can use `--height HEIGHT[%]` option not to start fzf in300 fullscreen mode.18 unmodified lines319 - `ALT-C` - cd into the selected directory320 - Set `FZF_ALT_C_COMMAND` to override the default command321 - Set `FZF_ALT_C_OPTS` to pass additional options322 321-If you're on a tmux session, you can start fzf in a split pane by setting322-`FZF_TMUX` to 1, and change the height of the pane with `FZF_TMUX_HEIGHT`323-(e.g. `20`, `50%`).323+If you're on a tmux session, you can start fzf in a tmux split pane or in324+a tmux popup window by setting `FZF_TMUX_OPTS` (e.g. `-d 40%`).325+See `fzf-tmux --help` for available options.326 327 More tips can be found on [the wiki page](https://github.com/junegunn/fzf/wiki/Configuring-shell-key-bindings).328 329 Fuzzy completion for bash and zsh325 unmodified lines