Skip to content
Myr

fzf-tmux

This file

Path

bin/fzf-tmux

257 lines · 7.2 KiB

Last changed by

fzf-tmux: validate attached layout size valueJunegunn Choibefore adoptionf126ff2

History of this file

Take it

1#!/usr/bin/env bash2# fzf-tmux: starts fzf in a tmux pane3# usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]45fail() {6  >&2 echo "$1"7  exit 28}910fzf="$(command which fzf)" || fzf="$(dirname "$0")/fzf"11[[ -x $fzf ]] || fail 'fzf executable not found'1213args=()14opt=""15skip=""16swap=""17close=""18term=""19[[ -n $LINES ]] && lines=$LINES || lines=$(tput lines) || lines=$(tmux display-message -p "#{pane_height}")20[[ -n $COLUMNS ]] && columns=$COLUMNS || columns=$(tput cols) || columns=$(tmux display-message -p "#{pane_width}")2122tmux_version=$(tmux -V | sed 's/[^0-9.]//g')23tmux_32=$(awk '{print ($1 >= 3.2)}' <<< "$tmux_version" 2> /dev/null || bc -l <<< "$tmux_version >= 3.2")2425help() {26  >&2 echo 'usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]2728  LAYOUT OPTIONS:29    (default layout: -d 50%)3031    Popup window (requires tmux 3.2 or above):32      -p [WIDTH[%][,HEIGHT[%]]]  (default: 50%)33      -w WIDTH[%]34      -h HEIGHT[%]35      -x COL36      -y ROW3738    Split pane:39      -u [HEIGHT[%]]             Split above (up)40      -d [HEIGHT[%]]             Split below (down)41      -l [WIDTH[%]]              Split left42      -r [WIDTH[%]]              Split right43'44  exit45}4647while [[ $# -gt 0 ]]; do48  arg="$1"49  shift50  [[ -z $skip ]] && case "$arg" in51    -)52      term=153      ;;54    --help)55      help56      ;;57    --version)58      echo "fzf-tmux (with fzf $("$fzf" --version))"59      exit60      ;;61    -p* | -w* | -h* | -x* | -y* | -d* | -u* | -r* | -l*)62      if [[ $arg =~ ^-[pwhxy] ]]; then63        [[ $opt =~ "-E" ]] || opt="-E"64      elif [[ $arg =~ ^.[lr] ]]; then65        opt="-h"66        if [[ $arg =~ ^.l ]]; then67          opt="$opt -d"68          swap="; swap-pane -D ; select-pane -L"69          close="; tmux swap-pane -D"70        fi71      else72        opt=""73        if [[ $arg =~ ^.u ]]; then74          opt="$opt -d"75          swap="; swap-pane -D ; select-pane -U"76          close="; tmux swap-pane -D"77        fi78      fi79      if [[ ${#arg} -gt 2 ]]; then80        size="${arg:2}"81        [[ $size =~ ^[0-9%,]+$ || $size =~ ^[A-Z]$ ]] || continue82      else83        if [[ $1 =~ ^[0-9%,]+$ ]] || [[ $1 =~ ^[A-Z]$ ]]; then84          size="$1"85          shift86        else87          continue88        fi89      fi9091      if [[ $arg =~ ^-p ]]; then92        if [[ -n $size ]]; then93          w=${size%%,*}94          h=${size##*,}95          opt="$opt -w$w -h$h"96        fi97      elif [[ $arg =~ ^-[whxy] ]]; then98        opt="$opt ${arg:0:2}$size"99      elif [[ $size =~ %$ ]]; then100        size=${size:0:${#size}-1}101        if [[ $tmux_32 == 1 ]]; then102          if [[ -n $swap ]]; then103            opt="$opt -l $((100 - size))%"104          else105            opt="$opt -l $size%"106          fi107        else108          if [[ -n $swap ]]; then109            opt="$opt -p $((100 - size))"110          else111            opt="$opt -p $size"112          fi113        fi114      else115        if [[ -n $swap ]]; then116          if [[ $arg =~ ^.l ]]; then117            max=$columns118          else119            max=$lines120          fi121          size=$((max - size))122          [[ $size -lt 0 ]] && size=0123          opt="$opt -l $size"124        else125          opt="$opt -l $size"126        fi127      fi128      ;;129    --)130      # "--" can be used to separate fzf-tmux options from fzf options to131      # avoid conflicts132      skip=1133      continue134      ;;135    *)136      args+=("$arg")137      ;;138  esac139  [[ -n $skip ]] && args+=("$arg")140done141142if [[ -z $TMUX ]]; then143  "$fzf" "${args[@]}"144  exit $?145fi146147# * --height option is not allowed148# * CTRL-Z is also disabled149# * fzf-tmux script is not compatible with --tmux option in fzf 0.53.0 or later150args=("${args[@]}" "--no-height" "--bind=ctrl-z:ignore" "--no-tmux")151152# Handle zoomed tmux pane without popup options by moving it to a temp window153if [[ ! $opt =~ "-E" ]] && tmux list-panes -F '#F' | grep -q Z; then154  zoomed_without_popup=1155  original_window=$(tmux display-message -p "#{window_id}")156  tmp_window=$(tmux new-window -d -P -F "#{window_id}" "bash -c 'while :; do for c in \\| / - '\\;' do sleep 0.2; printf \"\\r\$c fzf-tmux is running\\r\"; done; done'")157  tmux swap-pane -t $tmp_window \; select-window -t $tmp_window158fi159160set -e161162# Clean up named pipes on exit163tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/fzf-tmux-XXXXXX")164argsf="$tmpdir/args"165fifo1="$tmpdir/fifo1"166fifo2="$tmpdir/fifo2"167fifo3="$tmpdir/fifo3"168if tmux_win_opts=$(tmux show-options -p remain-on-exit \; show-options -p synchronize-panes 2> /dev/null); then169  tmux_win_opts=($(sed '/ off/d; s/synchronize-panes/set-option -p synchronize-panes/; s/remain-on-exit/set-option -p remain-on-exit/; s/$/ \\;/' <<< "$tmux_win_opts"))170  tmux_off_opts='; set-option -p synchronize-panes off ; set-option -p remain-on-exit off'171else172  tmux_win_opts=($(tmux show-window-options remain-on-exit \; show-window-options synchronize-panes | sed '/ off/d; s/^/set-window-option /; s/$/ \\;/'))173  tmux_off_opts='; set-window-option synchronize-panes off ; set-window-option remain-on-exit off'174fi175cleanup() {176  \rm -rf "$tmpdir"177  # Restore tmux window options178  if [[ ${#tmux_win_opts[@]} -gt 1 ]]; then179    eval "tmux ${tmux_win_opts[*]}"180  fi181182  # Remove temp window if we were zoomed without popup options183  if [[ -n $zoomed_without_popup ]]; then184    tmux display-message -p "#{window_id}" > /dev/null185    tmux swap-pane -t $original_window \; \186      select-window -t $original_window \; \187      kill-window -t $tmp_window \; \188      resize-pane -Z189  fi190191  if [[ $# -gt 0 ]]; then192    trap - EXIT193    exit 130194  fi195}196trap 'cleanup 1' SIGUSR1197trap 'cleanup' EXIT198199envs="export TERM=$(printf %q "$TERM") "200if [[ $opt =~ "-E" ]]; then201  if [[ $tmux_version == 3.2 ]]; then202    FZF_DEFAULT_OPTS="--margin 0,1 $FZF_DEFAULT_OPTS"203  elif [[ $tmux_32 == 1 ]]; then204    FZF_DEFAULT_OPTS="--border $FZF_DEFAULT_OPTS"205    opt="-B $opt"206  else207    echo "fzf-tmux: tmux 3.2 or above is required for popup mode" >&2208    exit 2209  fi210fi211envs="$envs FZF_DEFAULT_COMMAND=$(printf %q "$FZF_DEFAULT_COMMAND")"212envs="$envs FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS")"213envs="$envs FZF_DEFAULT_OPTS_FILE=$(printf %q "$FZF_DEFAULT_OPTS_FILE")"214[[ -n $RUNEWIDTH_EASTASIAN ]] && envs="$envs RUNEWIDTH_EASTASIAN=$(printf %q "$RUNEWIDTH_EASTASIAN")"215[[ -n $BAT_THEME ]] && envs="$envs BAT_THEME=$(printf %q "$BAT_THEME")"216echo "$envs;" > "$argsf"217218# Build arguments to fzf219opts=$(printf "%q " "${args[@]}")220221pppid=$$222echo -n "trap 'kill -SIGUSR1 -$pppid' EXIT SIGINT SIGTERM;" >> $argsf223close="; trap - EXIT SIGINT SIGTERM $close"224225export TMUX=$(cut -d , -f 1,2 <<< "$TMUX")226mkfifo -m o+w $fifo2227if [[ $opt =~ "-E" ]]; then228  cat $fifo2 &229  if [[ -n $term ]] || [[ -t 0 ]]; then230    cat <<< "\"$fzf\" $opts > $fifo2; out=\$? $close; exit \$out" >> $argsf231  else232    mkfifo $fifo1233    cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; out=\$? $close; exit \$out" >> $argsf234    cat <&0 > $fifo1 &235  fi236237  tmux popup -d "$PWD" $opt "bash $argsf" > /dev/null 2>&1238  exit $?239fi240241mkfifo -m o+w $fifo3242if [[ -n $term ]] || [[ -t 0 ]]; then243  cat <<< "\"$fzf\" $opts > $fifo2; echo \$? > $fifo3 $close" >> $argsf244else245  mkfifo $fifo1246  cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" >> $argsf247  cat <&0 > $fifo1 &248fi249tmux \250  split-window -c "$PWD" $opt "bash -c 'exec -a fzf bash $argsf'" $swap \251  $tmux_off_opts \252  > /dev/null 2>&1 || {253  "$fzf" "${args[@]}"254  exit $?255}256cat $fifo2257exit "$(cat $fifo3)"