From 7f48779a22ad3b440561762c2cd55ef89618f548 Mon Sep 17 00:00:00 2001 From: Gregory Pakosz Date: Thu, 25 May 2017 15:15:45 +0200 Subject: [PATCH] generalized options affecting bindings by parsing the output of tmux list-keys and reinjecting modified commands into tmux bind-key, closes #68 applies to: - tmux_conf_new_window_retain_current_path - tmux_conf_new_pane_retain_current_path - tmux_conf_new_pane_reconnect_ssh - tmux_conf_copy_to_os_clipboard --- .tmux.conf | 146 ++++++++++++++++++++++++++++------------------- .tmux.conf.local | 1 + 2 files changed, 87 insertions(+), 60 deletions(-) diff --git a/.tmux.conf b/.tmux.conf index 3f8d2ef..f8490d3 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -54,9 +54,17 @@ set -g visual-activity off # -- navigation ---------------------------------------------------------------- +# create session +bind C-c new-session + # find session bind C-f command-prompt -p find-session 'switch-client -t %%' +# split current window horizontally +bind - split-window -v +# split current window vertically +bind _ split-window -h + # pane navigation bind -r h select-pane -L # move left bind -r j select-pane -D # move down @@ -125,7 +133,7 @@ run -b 'tmux bind -T copy-mode-vi v send -X begin-selection 2> /dev/null || true run -b 'tmux bind -t vi-copy C-v rectangle-toggle 2> /dev/null || true' run -b 'tmux bind -T copy-mode-vi C-v send -X rectangle-toggle 2> /dev/null || true' run -b 'tmux bind -t vi-copy y copy-selection 2> /dev/null || true' -run -b 'tmux bind -T copy-mode-vi y send -X copy-selection 2> /dev/null || true' +run -b 'tmux bind -T copy-mode-vi y send -X copy-selection-and-cancel 2> /dev/null || true' run -b 'tmux bind -t vi-copy Escape cancel 2> /dev/null || true' run -b 'tmux bind -T copy-mode-vi Escape send -X cancel 2> /dev/null || true' run -b 'tmux bind -t vi-copy H start-of-line 2> /dev/null || true' @@ -577,47 +585,65 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration' # } # # _apply_bindings() { +# line=$(tmux list-keys | grep new-window | head -1) +# prefix=${line%new-window*} +# column=${#prefix} +# # tmux_conf_new_window_retain_current_path=${tmux_conf_new_window_retain_current_path:-false} -# if _is_enabled "$tmux_conf_new_window_retain_current_path"; then -# tmux bind c new-window -c '#{pane_current_path}' -# else -# tmux bind c new-window -# fi +# while IFS= read -r line; do +# [ -z "$line" ] && continue +# left=$(echo "$line" | cut -c-"$column" | sed -E -e 's/[^ \ta-zA-Z0-9,._+@%/-]/\\&/g') +# if _is_enabled "$tmux_conf_new_window_retain_current_path"; then +# right=$(echo "$line" | cut -c"$column-" | awk -F'\"' 'BEGIN { OFS = FS } { for (i = 1; i <= NF; i+=2) { gsub(/#{.+}/, "\"&\"", $i) } print }' | sed -E -e 's/new-window$/new-window -c "#{pane_current_path}"/g') +# else +# right=$(echo "$line" | cut -c"$column"- | awk -F'\"' 'BEGIN { OFS = FS } { for (i = 1; i <= NF; i+=2) { gsub(/#{.+}/, "\"&\"", $i) } print }' | sed -E -e 's/-c[ \t]+"#\{pane_current_path\}"$//g') +# fi +# eval "tmux $left $right" +# done << EOF +# $(tmux list-keys 2>/dev/null | grep -e 'new-window\(\s\+-c\s\+"#{pane_current_path}\|$\)') +# EOF # # tmux_conf_new_pane_retain_current_path=${tmux_conf_new_pane_retain_current_path:-true} +# while IFS= read -r line; do +# [ -z "$line" ] && continue +# left=$(echo "$line" | cut -c-"$column" | sed -E -e 's/[^ \ta-zA-Z0-9,._+@%/-]/\\&/g') +# if _is_enabled "$tmux_conf_new_pane_retain_current_path"; then +# right=$(echo "$line" | cut -c"$column-" | awk -F'\"' 'BEGIN { OFS = FS } { for (i = 1; i <= NF; i+=2) { gsub(/#{.+}/, "\"&\"", $i) } print }' | sed -E -e 's/split-window([ \t]+-(h|v))?$/& -c "#{pane_current_path}"/g') +# else +# right=$(echo "$line" | cut -c"$column"- | awk -F'\"' 'BEGIN { OFS = FS } { for (i = 1; i <= NF; i+=2) { gsub(/#{.+}/, "\"&\"", $i) } print }' | sed -E -e 's/-c[ \t]+"#\{pane_current_path\}"$//g') +# fi +# eval "tmux $left $right" +# done << EOF +# $(tmux list-keys 2>/dev/null | grep -e 'split\(-\|_\)window') +# EOF +# # tmux_conf_new_pane_reconnect_ssh=${tmux_conf_new_pane_reconnect_ssh:-false} -# if _is_enabled "$tmux_conf_new_pane_reconnect_ssh"; then -# if _is_enabled "$tmux_conf_new_pane_retain_current_path"; then -# tmux bind '"' run "cut -c3- ~/.tmux.conf | sh -s _split_window #{pane_tty} -v -c '#{pane_current_path}'" \;\ -# bind % run "cut -c3- ~/.tmux.conf | sh -s _split_window #{pane_tty} -h -c '#{pane_current_path}'" \;\ -# bind - run "cut -c3- ~/.tmux.conf | sh -s _split_window #{pane_tty} -v -c '#{pane_current_path}'" \;\ -# bind _ run "cut -c3- ~/.tmux.conf | sh -s _split_window #{pane_tty} -h -c '#{pane_current_path}'" +# while IFS= read -r line; do +# [ -z "$line" ] && continue +# left=$(echo "$line" | cut -c-"$column" | sed -E -e 's/[^ \ta-zA-Z0-9,._+@%/-]/\\&/g') +# if _is_enabled "$tmux_conf_new_pane_reconnect_ssh"; then +# right=$(echo "$line" | cut -c"$column"- | awk -F'\"' 'BEGIN { OFS = FS } { for (i = 1; i <= NF; i+=2) { gsub(/#{.+}/, "\"&\"", $i) } print }' | sed -E -e 's/split-window([^;]+)/run-shell "cut -c3- ~\/\.tmux\.conf | sh -s _split_window #{pane_tty}\1"/g') # else -# tmux bind '"' run "cut -c3- ~/.tmux.conf | sh -s _split_window #{pane_tty} -v" \;\ -# bind % run "cut -c3- ~/.tmux.conf | sh -s _split_window #{pane_tty} -h" \;\ -# bind - run "cut -c3- ~/.tmux.conf | sh -s _split_window #{pane_tty} -v" \;\ -# bind _ run "cut -c3- ~/.tmux.conf | sh -s _split_window #{pane_tty} -h" +# right=$(echo "$line" | cut -c"$column"- | awk -F'\"' 'BEGIN { OFS = FS } { for (i = 1; i <= NF; i+=2) { gsub(/#{.+}/, "\"&\"", $i) } print }' | sed -E -e 's/run-shell "cut -c3- ~\/\.tmux\.conf \| sh -s _split_window #\{pane_tty\}([^;]+)"/split-window\1/g' -e 's/#\{.+\}/\"&\"/g') # fi -# else -# if _is_enabled "$tmux_conf_new_pane_retain_current_path"; then -# tmux bind '"' split-window -v -c '#{pane_current_path}' \;\ -# bind % split-window -h -c '#{pane_current_path}' \;\ -# bind - split-window -v -c '#{pane_current_path}' \;\ -# bind _ split-window -h -c '#{pane_current_path}' -# else -# tmux bind '"' split-window -v \;\ -# bind % split-window -h \;\ -# bind - split-window -v \;\ -# bind _ split-window -h -# fi -# fi +# eval "tmux $left $right" +# done << EOF +# $(tmux list-keys 2>/dev/null | grep -e 'split\(-\|_\)window') +# EOF # # tmux_conf_new_session_prompt=${tmux_conf_new_session_prompt:-false} -# if _is_enabled "$tmux_conf_new_session_prompt"; then -# tmux bind C-c command-prompt -p new-session 'new-session -s "%%"' -# else -# tmux bind C-c new-session -# fi +# while IFS= read -r line; do +# [ -z "$line" ] && continue +# left=$(echo "$line" | cut -c-"$column" | sed -E -e 's/[^ \ta-zA-Z0-9,._+@%/-]/\\&/g') +# if _is_enabled "$tmux_conf_new_session_prompt"; then +# right=$(echo "$line" | cut -c"$column"- | awk -F'\"' 'BEGIN { OFS = FS } { for (i = 1; i <= NF; i+=2) { gsub(/#{.+}/, "\"&\"", $i) } print }' | sed -E -e 's/new-session$/command-prompt -p new-session \"new-session -s %%\"/g') +# else +# right=$(echo "$line" | cut -c"$column"- | awk -F'\"' 'BEGIN { OFS = FS } { for (i = 1; i <= NF; i+=2) { gsub(/#{.+}/, "\"&\"", $i) } print }'| sed -E -e 's/command-prompt -p new-session[^;]+/new-session/g') +# fi +# eval "tmux $left $right" +# done << EOF +# $(tmux list-keys 2>/dev/null | grep -e 'new-session') +# EOF # # if tmux -q -L swap-pane-test -f /dev/null new-session -d \; new-window \; new-window \; swap-pane -t :1 \; kill-session; then # tmux bind + run 'cut -c3- ~/.tmux.conf | sh -s _maximize_pane #{session_name} #D' @@ -626,32 +652,32 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration' # fi # # tmux_conf_copy_to_os_clipboard=${tmux_conf_copy_to_os_clipboard:-false} -# if _is_enabled "$tmux_conf_copy_to_os_clipboard"; then -# if command -v pbcopy > /dev/null 2>&1; then -# tmux bind -t vi-copy y copy-pipe 'pbcopy' 2> /dev/null || true -# tmux bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'pbcopy' 2> /dev/null || true -# fi -# if command -v reattach-to-user-namespace > /dev/null 2>&1; then -# tmux bind -t vi-copy y copy-pipe 'reattach-to-user-namespace pbcopy' 2> /dev/null || true -# tmux bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'reattach-to-user-namespace pbcopy' 2> /dev/null || true -# fi -# if command -v xsel > /dev/null 2>&1; then -# tmux bind -t vi-copy y copy-pipe 'xsel -i -b' 2> /dev/null || true -# tmux bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'xsel -i -b' 2> /dev/null || true -# fi -# if ! command -v xsel > /dev/null 2>&1 && command -v xclip > /dev/null 2>&1; then -# tmux bind -t vi-copy y copy-pipe 'xclip -i -selection clipboard > /dev/null 2>&1' 2> /dev/null || true -# tmux bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'xclip -i -selection clipboard > /dev/null 2>&1' 2> /dev/null || true -# fi -# if command -v clip.exe > /dev/null 2>&1; then -# tmux bind -t vi-copy y copy-pipe 'clip.exe' 2> /dev/null || true -# tmux bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'clip.exe' 2> /dev/null || true -# fi -# if [ -c /dev/clipboard ] > /dev/null 2>&1; then -# tmux bind -t vi-copy y copy-pipe 'cat > /dev/clipboard' 2> /dev/null || true -# tmux bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'cat > /dev/clipboard' 2> /dev/null || true -# fi -# fi +# command -v pbcopy > /dev/null 2>&1 && command='pbcopy' +# command -v reattach-to-user-namespace > /dev/null 2>&1 && command='reattach-to-user-namespace pbcopy' +# command -v xsel > /dev/null 2>&1 && command='xsel -i -b' +# ! command -v xsel > /dev/null 2>&1 && command -v xclip > /dev/null 2>&1 && command='xclip -i -selection clipboard > /dev/null 2>&1' +# command -v clip.exe > /dev/null 2>&1 && command='clip\.exe' +# [ -c /dev/clipboard ] && command='cat > \/dev\/clipboard' +# +# # shellcheck disable=SC2086 +# for table in "" "-t emacs-copy" "-t vi-copy"; do +# line=$(tmux list-keys $table 2>/dev/null | grep -e 'copy-selection\|copy-pipe' | head -1) +# prefix=${line%copy-*} +# column=${#prefix} +# +# while IFS= read -r line; do +# [ -z "$line" ] && continue +# left=$(echo "$line" | cut -c-"$column" | sed -E -e 's/[^ \ta-zA-Z0-9,._+@%/-]/\\&/g') +# if _is_enabled "$tmux_conf_copy_to_os_clipboard"; then +# right=$(echo "$line" | cut -c"$column"- | awk -F'\"' 'BEGIN { OFS = FS } { for (i = 1; i <= NF; i+=2) { gsub(/#{.+}/, "\"&\"", $i) } print }' | sed -E -e "s/copy-selection(-and-cancel)?$/copy-pipe\1 \"$command\"/g") +# else +# right=$(echo "$line" | cut -c"$column"- | awk -F'\"' 'BEGIN { OFS = FS } { for (i = 1; i <= NF; i+=2) { gsub(/#{.+}/, "\"&\"", $i) } print }' | sed -E -e "s/copy-pipe(-and-cancel)? \"$command\"$/copy-selection\1/g") +# fi +# eval "tmux $left $right" +# done << EOF +# $(tmux list-keys $table 2>/dev/null | grep -e 'copy-selection\|copy-pipe') +# EOF +# done # } # # _apply_theme() { diff --git a/.tmux.conf.local b/.tmux.conf.local index d187144..a54ed2b 100644 --- a/.tmux.conf.local +++ b/.tmux.conf.local @@ -234,6 +234,7 @@ tmux_conf_theme_clock_style='24' # in copy mode, copying selection also copies to the OS clipboard # - true # - false (default) +# on macOS, this require installing reattach-to-user-namespace, see README.md tmux_conf_copy_to_os_clipboard=false