diff --git a/.editorconfig b/.editorconfig index 34bf99c..d65b947 100644 --- a/.editorconfig +++ b/.editorconfig @@ -20,5 +20,5 @@ trim_trailing_whitespace = true [{*.sh,*.tmux}] indent_style = space -indent_size = 2 +indent_size = 4 trim_trailing_whitespace = true diff --git a/scripts/copy_line.sh b/scripts/copy_line.sh index d86ac40..be8d178 100755 --- a/scripts/copy_line.sh +++ b/scripts/copy_line.sh @@ -10,104 +10,104 @@ # sets a TMUX_COPY_MODE that is used as a global variable get_tmux_copy_mode() { - TMUX_COPY_MODE="$(tmux show-option -gwv mode-keys)" + TMUX_COPY_MODE="$(tmux show-option -gwv mode-keys)" } # The command when on ssh with latency. To make it work in this case too, # sleep is added. add_sleep_for_remote_shells() { - local pane_command="$(tmux display-message -p '#{pane_current_command}')" - if [[ "$pane_command" =~ (ssh|mosh) ]]; then - sleep "$REMOTE_SHELL_WAIT_TIME" - fi + local pane_command="$(tmux display-message -p '#{pane_current_command}')" + if [[ "$pane_command" =~ (ssh|mosh) ]]; then + sleep "$REMOTE_SHELL_WAIT_TIME" + fi } go_to_the_beginning_of_current_line() { - if [ "$(shell_mode)" == "emacs" ]; then - tmux send-key 'C-a' - else - tmux send-key 'Escape' '0' - fi - add_sleep_for_remote_shells + if [ "$(shell_mode)" == "emacs" ]; then + tmux send-key 'C-a' + else + tmux send-key 'Escape' '0' + fi + add_sleep_for_remote_shells } enter_tmux_copy_mode() { - tmux copy-mode + tmux copy-mode } start_tmux_selection() { - if tmux-is-at-least 2.4; then - echo "$TMUX_COPY_MODE" >> /tmp/tmux - tmux send -X begin-selection - elif [ "$TMUX_COPY_MODE" == "vi" ]; then - # vi copy mode - tmux send-key 'Space' - else - # emacs copy mode - tmux send-key 'C-Space' - fi + if tmux-is-at-least 2.4; then + echo "$TMUX_COPY_MODE" >> /tmp/tmux + tmux send -X begin-selection + elif [ "$TMUX_COPY_MODE" == "vi" ]; then + # vi copy mode + tmux send-key 'Space' + else + # emacs copy mode + tmux send-key 'C-Space' + fi } # works when command spans accross multiple lines end_of_line_in_copy_mode() { - if tmux-is-at-least 2.4; then - tmux send -X -N 150 'cursor-down' # 'down' key. 'vi' mode is faster so we're - # jumping more lines than emacs. - tmux send -X 'end-of-line' # End of line (just in case we are already at the last line). - tmux send -X 'previous-word' # Beginning of the previous word. - tmux send -X 'next-word-end' # End of next word. - elif [ "$TMUX_COPY_MODE" == "vi" ]; then - # vi copy mode - # This sequence of keys consistently selects multiple lines - tmux send-key '150' # Go to the bottom of scrollback buffer by using - tmux send-key 'j' # 'down' key. 'vi' mode is faster so we're - # jumping more lines than emacs. - tmux send-key '$' # End of line (just in case we are already at the last line). - tmux send-key 'b' # Beginning of the previous word. - tmux send-key 'e' # End of next word. - else - # emacs copy mode - for (( c=1; c<='30'; c++ )); do # go to the bottom of scrollback buffer - tmux send-key 'C-n' - done - tmux send-key 'C-e' - tmux send-key 'M-b' - tmux send-key 'M-f' - fi + if tmux-is-at-least 2.4; then + tmux send -X -N 150 'cursor-down' # 'down' key. 'vi' mode is faster so we're + # jumping more lines than emacs. + tmux send -X 'end-of-line' # End of line (just in case we are already at the last line). + tmux send -X 'previous-word' # Beginning of the previous word. + tmux send -X 'next-word-end' # End of next word. + elif [ "$TMUX_COPY_MODE" == "vi" ]; then + # vi copy mode + # This sequence of keys consistently selects multiple lines + tmux send-key '150' # Go to the bottom of scrollback buffer by using + tmux send-key 'j' # 'down' key. 'vi' mode is faster so we're + # jumping more lines than emacs. + tmux send-key '$' # End of line (just in case we are already at the last line). + tmux send-key 'b' # Beginning of the previous word. + tmux send-key 'e' # End of next word. + else + # emacs copy mode + for (( c=1; c<='30'; c++ )); do # go to the bottom of scrollback buffer + tmux send-key 'C-n' + done + tmux send-key 'C-e' + tmux send-key 'M-b' + tmux send-key 'M-f' + fi } yank_to_clipboard() { - if tmux-is-at-least 2.4; then - tmux send -X copy-pipe-and-cancel "$(clipboard_copy_command)" - else - tmux send-key "$(yank_wo_newline_key)" - fi + if tmux-is-at-least 2.4; then + tmux send -X copy-pipe-and-cancel "$(clipboard_copy_command)" + else + tmux send-key "$(yank_wo_newline_key)" + fi } go_to_the_end_of_current_line() { - if [ "$(shell_mode)" == "emacs" ]; then - tmux send-keys 'C-e' - else - tmux send-keys '$' 'a' - fi + if [ "$(shell_mode)" == "emacs" ]; then + tmux send-keys 'C-e' + else + tmux send-keys '$' 'a' + fi } display_notice() { - display_message 'Line copied to clipboard!' + display_message 'Line copied to clipboard!' } yank_current_line() { - go_to_the_beginning_of_current_line - enter_tmux_copy_mode - start_tmux_selection - end_of_line_in_copy_mode - yank_to_clipboard - go_to_the_end_of_current_line - display_notice + go_to_the_beginning_of_current_line + enter_tmux_copy_mode + start_tmux_selection + end_of_line_in_copy_mode + yank_to_clipboard + go_to_the_end_of_current_line + display_notice } main() { - get_tmux_copy_mode - yank_current_line + get_tmux_copy_mode + yank_current_line } main diff --git a/scripts/copy_pane_pwd.sh b/scripts/copy_pane_pwd.sh index b7f95f7..5c69a2d 100755 --- a/scripts/copy_pane_pwd.sh +++ b/scripts/copy_pane_pwd.sh @@ -6,17 +6,17 @@ source "$HELPERS_DIR/helpers.sh" pane_current_path() { - tmux display -p -F "#{pane_current_path}" + tmux display -p -F "#{pane_current_path}" } display_notice() { - display_message 'PWD copied to clipboard!' + display_message 'PWD copied to clipboard!' } main() { - local copy_command="$(clipboard_copy_command)" - # $copy_command below should not be quoted - pane_current_path | tr -d '\n' | $copy_command - display_notice + local copy_command="$(clipboard_copy_command)" + # $copy_command below should not be quoted + pane_current_path | tr -d '\n' | $copy_command + display_notice } main diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 22aaf8c..8178f46 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -27,128 +27,128 @@ # helper functions get_tmux_option() { - local option="$1" - local default_value="$2" - local option_value=$(tmux show-option -gqv "$option") - if [ -z "$option_value" ]; then - echo "$default_value" - else - echo "$option_value" - fi + local option="$1" + local default_value="$2" + local option_value=$(tmux show-option -gqv "$option") + if [ -z "$option_value" ]; then + echo "$default_value" + else + echo "$option_value" + fi } yank_line_key() { - echo "$(get_tmux_option "$yank_line_option" "$yank_line")" + echo "$(get_tmux_option "$yank_line_option" "$yank_line")" } yank_pane_pwd_key() { - echo "$(get_tmux_option "$yank_pane_pwd_option" "$yank_pane_pwd")" + echo "$(get_tmux_option "$yank_pane_pwd_option" "$yank_pane_pwd")" } yank_key() { - echo "$(get_tmux_option "$yank_option" "$yank_default")" + echo "$(get_tmux_option "$yank_option" "$yank_default")" } put_key() { - echo "$(get_tmux_option "$put_option" "$put_default")" + echo "$(get_tmux_option "$put_option" "$put_default")" } yank_put_key() { - echo "$(get_tmux_option "$yank_put_option" "$yank_put_default")" + echo "$(get_tmux_option "$yank_put_option" "$yank_put_default")" } yank_wo_newline_key() { - echo "$(get_tmux_option "$yank_wo_newline_option" "$yank_wo_newline_default")" + echo "$(get_tmux_option "$yank_wo_newline_option" "$yank_wo_newline_default")" } yank_selection() { - echo "$(get_tmux_option "$yank_selection_option" "$yank_selection_default")" + echo "$(get_tmux_option "$yank_selection_option" "$yank_selection_default")" } shell_mode() { - echo "$(get_tmux_option "$shell_mode_option" "$shell_mode_default")" + echo "$(get_tmux_option "$shell_mode_option" "$shell_mode_default")" } custom_copy_command() { - echo "$(get_tmux_option "$custom_copy_command_option" "$custom_copy_command_default")" + echo "$(get_tmux_option "$custom_copy_command_option" "$custom_copy_command_default")" } # Ensures a message is displayed for 5 seconds in tmux prompt. # Does not override the 'display-time' tmux option. display_message() { - local message="$1" + local message="$1" - # display_duration defaults to 5 seconds, if not passed as an argument - if [ "$#" -eq 2 ]; then - local display_duration="$2" - else - local display_duration="5000" - fi + # display_duration defaults to 5 seconds, if not passed as an argument + if [ "$#" -eq 2 ]; then + local display_duration="$2" + else + local display_duration="5000" + fi - # saves user-set 'display-time' option - local saved_display_time=$(get_tmux_option "display-time" "750") + # saves user-set 'display-time' option + local saved_display_time=$(get_tmux_option "display-time" "750") - # sets message display time to 5 seconds - tmux set-option -gq display-time "$display_duration" + # sets message display time to 5 seconds + tmux set-option -gq display-time "$display_duration" - # displays message - tmux display-message "$message" + # displays message + tmux display-message "$message" - # restores original 'display-time' value - tmux set-option -gq display-time "$saved_display_time" + # restores original 'display-time' value + tmux set-option -gq display-time "$saved_display_time" } command_exists() { - local command="$1" - type "$command" >/dev/null 2>&1 + local command="$1" + type "$command" >/dev/null 2>&1 } clipboard_copy_command() { - # installing reattach-to-user-namespace is recommended on OS X - if command_exists "pbcopy"; then - if command_exists "reattach-to-user-namespace"; then - echo "reattach-to-user-namespace pbcopy" - else - echo "pbcopy" - fi - elif command_exists "clip.exe"; then # WSL clipboard command - echo "clip.exe" - elif command_exists "xclip"; then - local xclip_selection="$(yank_selection)" - echo "xclip -selection $xclip_selection" - elif command_exists "xsel"; then - local xsel_selection="$(yank_selection)" - echo "xsel -i --$xsel_selection" - elif command_exists "putclip"; then # cygwin clipboard command - echo "putclip" - elif [ -n "$(custom_copy_command)" ]; then - echo "$(custom_copy_command)" - fi + # installing reattach-to-user-namespace is recommended on OS X + if command_exists "pbcopy"; then + if command_exists "reattach-to-user-namespace"; then + echo "reattach-to-user-namespace pbcopy" + else + echo "pbcopy" + fi + elif command_exists "clip.exe"; then # WSL clipboard command + echo "clip.exe" + elif command_exists "xclip"; then + local xclip_selection="$(yank_selection)" + echo "xclip -selection $xclip_selection" + elif command_exists "xsel"; then + local xsel_selection="$(yank_selection)" + echo "xsel -i --$xsel_selection" + elif command_exists "putclip"; then # cygwin clipboard command + echo "putclip" + elif [ -n "$(custom_copy_command)" ]; then + echo "$(custom_copy_command)" + fi } tmux_version="$(tmux -V | cut -d ' ' -f 2)" tmux-is-at-least() { - if [[ $tmux_version == $1 ]] - then - return 0 - fi +if [[ $tmux_version == $1 ]] +then + return 0 +fi - local IFS=. - local i tver=($tmux_version) wver=($1) +local IFS=. +local i tver=($tmux_version) wver=($1) - # fill empty fields in tver with zeros - for ((i=${#tver[@]}; i<${#wver[@]}; i++)); do - tver[i]=0 - done +# fill empty fields in tver with zeros +for ((i=${#tver[@]}; i<${#wver[@]}; i++)); do + tver[i]=0 +done - # fill empty fields in wver with zeros - for ((i=${#wver[@]}; i<${#tver[@]}; i++)); do - wver[i]=0 - done +# fill empty fields in wver with zeros +for ((i=${#wver[@]}; i<${#tver[@]}; i++)); do + wver[i]=0 +done - for ((i=0; i<${#tver[@]}; i++)); do - if ((10#${tver[i]} < 10#${wver[i]})); then - return 1 - fi - done - return 0 +for ((i=0; i<${#tver[@]}; i++)); do + if ((10#${tver[i]} < 10#${wver[i]})); then + return 1 + fi +done +return 0 } diff --git a/yank.tmux b/yank.tmux index 8d53afb..d1a6cf9 100755 --- a/yank.tmux +++ b/yank.tmux @@ -7,69 +7,69 @@ source "$HELPERS_DIR/helpers.sh" clipboard_copy_without_newline_command() { - local copy_command="$1" - echo "tr -d '\n' | $copy_command" + local copy_command="$1" + echo "tr -d '\n' | $copy_command" } set_error_bindings() { - local key_bindings="$(yank_key) $(put_key) $(yank_put_key)" - local key - for key in $key_bindings; do - if tmux-is-at-least 2.4; then - tmux bind-key -t copy-mode-vi "$key" send-keys -X copy-pipe-and-cancel "tmux display-message 'Error! tmux-yank dependencies not installed!'" - tmux bind-key -t copy-mode "$key" send-keys -X copy-pipe-and-cancel "tmux display-message 'Error! tmux-yank dependencies not installed!'" - else - tmux bind-key -t vi-copy "$key" copy-pipe "tmux display-message 'Error! tmux-yank dependencies not installed!'" - tmux bind-key -t emacs-copy "$key" copy-pipe "tmux display-message 'Error! tmux-yank dependencies not installed!'" - fi - done + local key_bindings="$(yank_key) $(put_key) $(yank_put_key)" + local key + for key in $key_bindings; do + if tmux-is-at-least 2.4; then + tmux bind-key -t copy-mode-vi "$key" send-keys -X copy-pipe-and-cancel "tmux display-message 'Error! tmux-yank dependencies not installed!'" + tmux bind-key -t copy-mode "$key" send-keys -X copy-pipe-and-cancel "tmux display-message 'Error! tmux-yank dependencies not installed!'" + else + tmux bind-key -t vi-copy "$key" copy-pipe "tmux display-message 'Error! tmux-yank dependencies not installed!'" + tmux bind-key -t emacs-copy "$key" copy-pipe "tmux display-message 'Error! tmux-yank dependencies not installed!'" + fi + done } error_handling_if_command_not_present() { - local copy_command="$1" - if [ -z "$copy_command" ]; then - set_error_bindings - exit 0 - fi + local copy_command="$1" + if [ -z "$copy_command" ]; then + set_error_bindings + exit 0 + fi } # `yank_without_newline` binding isn't intended to be used by the user. It is # a helper for `copy_line` command. set_copy_mode_bindings() { - local copy_command="$1" - local copy_wo_newline_command="$(clipboard_copy_without_newline_command "$copy_command")" - if tmux-is-at-least 2.4; then - tmux bind-key -T copy-mode-vi "$(yank_key)" send-keys -X copy-pipe-and-cancel "$copy_command" - tmux bind-key -T copy-mode-vi "$(put_key)" send-keys -X copy-pipe-and-cancel "tmux paste-buffer" - tmux bind-key -T copy-mode-vi "$(yank_put_key)" send-keys -X copy-pipe-and-cancel "$copy_command; tmux paste-buffer" - tmux bind-key -T copy-mode-vi "$(yank_wo_newline_key)" send-keys -X copy-pipe-and-cancel "$copy_wo_newline_command" + local copy_command="$1" + local copy_wo_newline_command="$(clipboard_copy_without_newline_command "$copy_command")" + if tmux-is-at-least 2.4; then + tmux bind-key -T copy-mode-vi "$(yank_key)" send-keys -X copy-pipe-and-cancel "$copy_command" + tmux bind-key -T copy-mode-vi "$(put_key)" send-keys -X copy-pipe-and-cancel "tmux paste-buffer" + tmux bind-key -T copy-mode-vi "$(yank_put_key)" send-keys -X copy-pipe-and-cancel "$copy_command; tmux paste-buffer" + tmux bind-key -T copy-mode-vi "$(yank_wo_newline_key)" send-keys -X copy-pipe-and-cancel "$copy_wo_newline_command" - tmux bind-key -T copy-mode "$(yank_key)" send-keys -X copy-pipe-and-cancel "$copy_command" - tmux bind-key -T copy-mode "$(put_key)" send-keys -X copy-pipe-and-cancel "tmux paste-buffer" - tmux bind-key -T copy-mode "$(yank_put_key)" send-keys -X copy-pipe-and-cancel "$copy_command; tmux paste-buffer" - tmux bind-key -T copy-mode "$(yank_wo_newline_key)" send-keys -X copy-pipe-and-cancel "$copy_wo_newline_command" - else - tmux bind-key -t vi-copy "$(yank_key)" copy-pipe "$copy_command" - tmux bind-key -t vi-copy "$(put_key)" copy-pipe "tmux paste-buffer" - tmux bind-key -t vi-copy "$(yank_put_key)" copy-pipe "$copy_command; tmux paste-buffer" - tmux bind-key -t vi-copy "$(yank_wo_newline_key)" copy-pipe "$copy_wo_newline_command" + tmux bind-key -T copy-mode "$(yank_key)" send-keys -X copy-pipe-and-cancel "$copy_command" + tmux bind-key -T copy-mode "$(put_key)" send-keys -X copy-pipe-and-cancel "tmux paste-buffer" + tmux bind-key -T copy-mode "$(yank_put_key)" send-keys -X copy-pipe-and-cancel "$copy_command; tmux paste-buffer" + tmux bind-key -T copy-mode "$(yank_wo_newline_key)" send-keys -X copy-pipe-and-cancel "$copy_wo_newline_command" + else + tmux bind-key -t vi-copy "$(yank_key)" copy-pipe "$copy_command" + tmux bind-key -t vi-copy "$(put_key)" copy-pipe "tmux paste-buffer" + tmux bind-key -t vi-copy "$(yank_put_key)" copy-pipe "$copy_command; tmux paste-buffer" + tmux bind-key -t vi-copy "$(yank_wo_newline_key)" copy-pipe "$copy_wo_newline_command" - tmux bind-key -t emacs-copy "$(yank_key)" copy-pipe "$copy_command" - tmux bind-key -t emacs-copy "$(put_key)" copy-pipe "tmux paste-buffer" - tmux bind-key -t emacs-copy "$(yank_put_key)" copy-pipe "$copy_command; tmux paste-buffer" - tmux bind-key -t emacs-copy "$(yank_wo_newline_key)" copy-pipe "$copy_wo_newline_command" - fi + tmux bind-key -t emacs-copy "$(yank_key)" copy-pipe "$copy_command" + tmux bind-key -t emacs-copy "$(put_key)" copy-pipe "tmux paste-buffer" + tmux bind-key -t emacs-copy "$(yank_put_key)" copy-pipe "$copy_command; tmux paste-buffer" + tmux bind-key -t emacs-copy "$(yank_wo_newline_key)" copy-pipe "$copy_wo_newline_command" + fi } set_normal_bindings() { - tmux bind-key "$(yank_line_key)" run-shell -b "$SCRIPTS_DIR/copy_line.sh" - tmux bind-key "$(yank_pane_pwd_key)" run-shell -b "$SCRIPTS_DIR/copy_pane_pwd.sh" + tmux bind-key "$(yank_line_key)" run-shell -b "$SCRIPTS_DIR/copy_line.sh" + tmux bind-key "$(yank_pane_pwd_key)" run-shell -b "$SCRIPTS_DIR/copy_pane_pwd.sh" } main() { - local copy_command="$(clipboard_copy_command)" - error_handling_if_command_not_present "$copy_command" - set_copy_mode_bindings "$copy_command" - set_normal_bindings + local copy_command="$(clipboard_copy_command)" + error_handling_if_command_not_present "$copy_command" + set_copy_mode_bindings "$copy_command" + set_normal_bindings } main