Skip to content
Myr

[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

9 files+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]45fail() {6 >&2 echo "$1"7 exit 2
1 unmodified line
910fzf="$(command -v fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf"11[[ -x "$fzf" ]] || fail 'fzf executable not found'1213tmux_args=()14args=()15opt=""16skip=""17swap=""
2 unmodified lines
20[[ -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}")2223help() {23 >&2 echo 'usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]24 >&2 echo 'usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]2525 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%)2831 (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 ROW3536 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 lines
55 --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 lines
76 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 fi8777 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 else
17 unmodified lines
118 --)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 lines
135 exit $?136fi137138# --height option is not allowed118args+=("--no-height")139args=("--no-height" "${args[@]}")140141# Handle zoomed tmux pane by moving it to a temp window142if tmux list-panes -F '#F' | grep -q Z; then143 zoomed=1
57 unmodified lines
201close="; trap - EXIT SIGINT SIGTERM $close"202203tmux_win_opts=( $(tmux show-window-options remain-on-exit \; show-window-options synchronize-panes | sed '/ off/d; s/^/set-window-option /; s/$/ \\;/') )204205if [[ "$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 $?217fi218219if [[ -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>&1225else226 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 &233fi234cat $fifo2235exit "$(cat $fifo3)"201
modified:man/man1/fzf-tmux.1+22 added-8 removed
25 unmodified lines
26.SH NAME27fzf-tmux - open fzf in tmux split pane2829.SH SYNOPSIS30.B fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]30.B fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]3132.SH DESCRIPTION33fzf-tmux is a wrapper script for fzf that opens fzf in a tmux split pane. It is34designed to work just like fzf except that it does not take up the whole35screen. You can safely use fzf-tmux instead of fzf in your scripts as the extra36options will be silently ignored if you're not on tmux.33fzf-tmux is a wrapper script for fzf that opens fzf in a tmux split pane or in34a tmux popup window. It is designed to work just like fzf except that it does35not take up the whole screen. You can safely use fzf-tmux instead of fzf in36your scripts as the extra options will be silently ignored if you're not on37tmux.3838.SH OPTIONS39.SS Layout39.SH LAYOUT OPTIONS4041(default: \fB-d 50%\fR)41(default layout: \fB-d 50%\fR)4243.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"5556.SS Split pane57.TP58.B "-u [height[%]]"59Split above (up)60.TP61.B "-d [height[%]]"
7 unmodified lines
modified: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)1112if [[ $- =~ i ]]; then
23 unmodified lines
3637__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 fi
319 unmodified lines
modified: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)1112# Both branches of the following `if` do the same thing -- define
85 unmodified lines
9899__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 fi
205 unmodified lines
modified:shell/key-bindings.bash+20 added-31 removed
modified: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_OPTS1314# Key bindings15# ------------16function fzf_key_bindings17
78 unmodified lines
9697 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 end
54 unmodified lines
modified: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_OPTS1314# Key bindings15# ------------1617# The code at the top and the bottom of this file is the same as in completion.zsh.
34 unmodified lines
52 echo53 return $ret54}5543__fzf_use_tmux__() {44 [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]45}4656__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}6061fzf-file-widget() {62 LBUFFER="${LBUFFER}$(__fsel)"
59 unmodified lines
modified:test/test_go.rb+13 added-15 removed
97 unmodified lines
98 def kill99 go(%W[kill-window -t #{win}])100 end101102 def focus103 go(%W[select-window -t #{win}])104 end105106 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 end109110 def paste(str)111 system('tmux', 'setb', str, ';', 'pasteb', '-t', win, ';', 'send-keys', '-t', win, 'Enter')112 end113118 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 end117122 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 lines
2035 tmux.until { |lines| lines[-1].include? 'unset FZFFOOBAR' }2036 tmux.send_keys 'C-c'20372038 # 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 lines
modified:README.md+8 added-6 removed
279 unmodified lines
280281[fzf-tmux](bin/fzf-tmux) is a bash script that opens fzf in a tmux pane.282283```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]285286# See available options287fzf-tmux --help288289# select git branches in horizontal split below (15 lines)290git branch | fzf-tmux -d 15291292# select multiple words in vertical split on the left (20% of screen width)293cat /usr/share/dict/words | fzf-tmux -l 20% --multi --reverse294```295294It will still work even when you're not on tmux, silently ignoring `-[udlr]`296It will still work even when you're not on tmux, silently ignoring `-[pudlr]`297options, so you can invariably use `fzf-tmux` in your scripts.298299Alternatively, you can use `--height HEIGHT[%]` option not to start fzf in300fullscreen mode.
18 unmodified lines
319- `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 options322321If 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%`).323If you're on a tmux session, you can start fzf in a tmux split pane or in324a tmux popup window by setting `FZF_TMUX_OPTS` (e.g. `-d 40%`).325See `fzf-tmux --help` for available options.326327More tips can be found on [the wiki page](https://github.com/junegunn/fzf/wiki/Configuring-shell-key-bindings).328329Fuzzy completion for bash and zsh
325 unmodified lines