Skip to content

Commit 64e01e6

Browse files
committed
Copy on MouseDragEnd event but do not reset copy mode and selection. Add shortcut to copy whole line. Strip trailing new lines from copied text
1 parent af2efd9 commit 64e01e6

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,11 @@ There is a root keybinding to enter Copy mode: `M-Up`. Once in copy mode, you ha
358358

359359
`Space` starts selection, `Enter` copies selection and exits copy mode. List all items in copy buffer using `prefix C-p`, and paste most recent item from buffer using `prexix p`.
360360

361-
You can also use `y` which is equivalent to `Enter`, and `Y` will copy text, exit copy mode and immediately paste buffer to you command line.
361+
`y` just copies selected text and is equivalent to `Enter`, `Y` copies whole line, and `D` copies by the end of line.
362362

363-
You can also select text using mouse. Default behavior is to copy text and immediately cancel copy mode on `MouseDragEnd` event. This is annoying, because sometimes I select text just to highlight it, but tmux drops me out of copy mode and reset scroll by the end. I've changed this behavior, so `MouseDragEnd` does not execute `copy-selection-and-cancel` action. You can then copy text just by left mouse click, and continue working in copy-mode.
363+
Also, note, that when text is copied any trailing new lines are stripped. So, when you paste buffer in a command prompt, it will not be immediately executed.
364+
365+
You can also select text using mouse. Default behavior is to copy text and immediately cancel copy mode on `MouseDragEnd` event. This is annoying, because sometimes I select text just to highlight it, but tmux drops me out of copy mode and reset scroll by the end. I've changed this behavior, so `MouseDragEnd` does not execute `copy-selection-and-cancel` action. Text is copied, but copy mode is not cancelled and selection is not cleared. You can then reset selection by mouse click.
364366

365367
![copy and scroll](https://user-images.githubusercontent.com/768858/33231146-e390afc8-d1f8-11e7-80ad-6977fc3a5df7.gif)
366368

tmux/tmux.conf

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,22 @@ if -b "command -v reattach-to-user-namespace > /dev/null 2>&1" \
175175

176176
yank="~/.tmux/yank.sh"
177177

178-
# Remap keys which perform copy to pipe copied text to OS clipboard
178+
# Copy selected text
179179
bind -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "$yank"
180180
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "$yank"
181-
bind -T copy-mode-vi Y send-keys -X copy-pipe-and-cancel "$yank; tmux paste-buffer"
182-
bind -T copy-mode-vi C-j send-keys -X copy-pipe-and-cancel "$yank"
181+
bind -T copy-mode-vi Y send-keys -X copy-line \;\
182+
run "tmux save-buffer - | $yank"
183183
bind-key -T copy-mode-vi D send-keys -X copy-end-of-line \;\
184184
run "tmux save-buffer - | $yank"
185+
bind -T copy-mode-vi C-j send-keys -X copy-pipe-and-cancel "$yank"
185186
bind-key -T copy-mode-vi A send-keys -X append-selection-and-cancel \;\
186187
run "tmux save-buffer - | $yank"
187188

188-
# Do not copy selection and cancel copy mode on drag end event
189-
# Prefer iTerm style selection: select, then mouse click to copy to buffer
190-
unbind -T copy-mode-vi MouseDragEnd1Pane
189+
# Copy selection on drag end event, but do not cancel copy mode and do not clear selection
190+
# clear select on subsequence mouse click
191+
bind -T copy-mode-vi MouseDragEnd1Pane \
192+
send-keys -X copy-pipe "$yank"
191193
bind -T copy-mode-vi MouseDown1Pane select-pane \;\
192-
send-keys -X copy-pipe "$yank" \;\
193194
send-keys -X clear-selection
194195

195196
# iTerm2 works with clipboard out of the box, set-clipboard already set to "external"
@@ -268,6 +269,7 @@ wg_battery="#{battery_status_fg} #{battery_icon} #{battery_percentage}"
268269
wg_date="#[fg=$color_secondary]%h %d %H:%M#[default]"
269270
wg_user_host="#[fg=$color_secondary]#(echo $USER)#[default]@#H"
270271
wg_is_zoomed="#[fg=$color_dark,bg=$color_secondary]#{?window_zoomed_flag,[Z],}#[default]"
272+
# TODO: highlighted for nested local session as well
271273
wg_is_keys_off="#[fg=$color_light,bg=$color_window_off_indicator]#([ $(tmux show-option -qv key-table) = 'off' ] && echo 'OFF')#[default]"
272274

273275
set -g status-left "$wg_session"

tmux/yank.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ is_app_installed() {
77
}
88

99
# get data either form stdin or from file
10-
buf=$(cat "$@")
10+
# 'echo -n' to strip any trailing new lines, so when we paste in command prompt
11+
# it won't be executed immediately
12+
buf=$(cat "$@" | xargs echo -n)
1113

1214
copy_backend_remote_tunnel_port=$(tmux show-option -gvq "@copy_backend_remote_tunnel_port")
1315
copy_use_osc52_fallback=$(tmux show-option -gvq "@copy_use_osc52_fallback")
@@ -28,7 +30,7 @@ fi
2830

2931
# if copy backend is resolved, copy and exit
3032
if [ -n "$copy_backend" ]; then
31-
printf "$buf" | eval "$copy_backend"
33+
printf "$buf" | eval "$copy_backend"
3234
exit;
3335
fi
3436

0 commit comments

Comments
 (0)