Skip to content

Commit 30bfd78

Browse files
committed
Merge yank-with-mouse from @keidax
- Ran `shfmt` on shell code. - Fixed ordering of `xclip` and `xsel`. Issue #109
2 parents c6a73eb + 74b0ff7 commit 30bfd78

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Change Log
44
[master]
55
--------
66

7+
### Added
8+
9+
- Mouse support, controlled by `yank_with_mouse` and `yank_selection_mouse`
10+
(@keidax)
11+
712
[v2.3.0] 2018-02-01
813
-------------------
914

README.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,48 @@ You can change this by setting `@yank_selection`:
209209
set -g @yank_selection 'primary' # or 'secondary' or 'clipboard'
210210
```
211211
212+
With mouse support turned on (see below) the default clipboard for mouse
213+
selections is `primary`.
214+
215+
You can change this by setting `@yank_selection_mouse`:
216+
217+
``` tmux
218+
# ~/.tmux.conf
219+
220+
set -g @yank_selection_mouse 'clipboard' # or 'primary' or 'secondary'
221+
```
222+
223+
### Controlling Yank Behavior
224+
225+
By default, `tmux-yank` will exit copy mode after yanking text. If you wish to
226+
remain in copy mode, you can set `@yank_action`:
227+
228+
``` tmux
229+
# ~/.tmux.conf
230+
231+
set -g @yank_action 'copy-pipe' # or 'copy-pipe-and-cancel' for the default
232+
```
233+
212234
### Mouse Support
213235
214-
When making a selection using `tmux` with `mode-mouse on` or
215-
`mode-mouse copy-mode`, you cannot rely on the default 'release mouse after
216-
selection to copy' behavior.
236+
`tmux-yank` has mouse support enabled by default. It will only work if `tmux`'s
237+
built-in mouse support is also enabled (with `mouse on` since `tmux` 2.1, or
238+
`mode-mouse on` in older versions).
239+
240+
To yank with the mouse, click and drag with the primary button to begin
241+
selection, and release to yank.
242+
243+
If you would prefer to disable this behavior, or provide your own bindings for
244+
the `MouseDragEnd1Pane` event, you can do so with:
245+
246+
``` tmux
247+
# ~/.tmux.conf
248+
249+
set -g @yank_with_mouse off # or 'on'
250+
```
217251

218-
Instead, press <kbd>y</kbd> before releasing mouse.
252+
If you want to remain in copy mode after making a mouse selection, set
253+
`@yank_action` as described above.
219254

220255
### vi mode support
221256

scripts/helpers.sh

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ yank_wo_newline_option="@copy_mode_yank_wo_newline"
2121
yank_selection_default="clipboard"
2222
yank_selection_option="@yank_selection"
2323

24+
yank_selection_mouse_default="primary"
25+
yank_selection_mouse_option="@yank_selection_mouse"
26+
27+
yank_with_mouse_default="on"
28+
yank_with_mouse_option="@yank_with_mouse"
29+
2430
yank_action_default="copy-pipe-and-cancel"
2531
yank_action_option="@yank_action"
2632

@@ -74,6 +80,14 @@ yank_selection() {
7480
get_tmux_option "$yank_selection_option" "$yank_selection_default"
7581
}
7682

83+
yank_selection_mouse() {
84+
get_tmux_option "$yank_selection_mouse_option" "$yank_selection_mouse_default"
85+
}
86+
87+
yank_with_mouse() {
88+
get_tmux_option "$yank_with_mouse_option" "$yank_with_mouse_default"
89+
}
90+
7791
yank_action() {
7892
get_tmux_option "$yank_action_option" "$yank_action_default"
7993
}
@@ -121,6 +135,7 @@ command_exists() {
121135
}
122136

123137
clipboard_copy_command() {
138+
local mouse="${1:-false}"
124139
# installing reattach-to-user-namespace is recommended on OS X
125140
if [ -n "$(override_copy_command)" ]; then
126141
override_copy_command
@@ -134,11 +149,19 @@ clipboard_copy_command() {
134149
echo "clip.exe"
135150
elif command_exists "xsel"; then
136151
local xsel_selection
137-
xsel_selection="$(yank_selection)"
152+
if [[ $mouse == "true" ]]; then
153+
xsel_selection="$(yank_selection_mouse)"
154+
else
155+
xsel_selection="$(yank_selection)"
156+
fi
138157
echo "xsel -i --$xsel_selection"
139158
elif command_exists "xclip"; then
140159
local xclip_selection
141-
xclip_selection="$(yank_selection)"
160+
if [[ $mouse == "true" ]]; then
161+
xclip_selection="$(yank_selection_mouse)"
162+
else
163+
xclip_selection="$(yank_selection)"
164+
fi
142165
echo "xclip -selection $xclip_selection"
143166
elif command_exists "putclip"; then # cygwin clipboard command
144167
echo "putclip"

yank.tmux

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,40 @@ set_copy_mode_bindings() {
4040
local copy_command="$1"
4141
local copy_wo_newline_command
4242
copy_wo_newline_command="$(clipboard_copy_without_newline_command "$copy_command")"
43+
local copy_command_mouse
44+
copy_command_mouse="$(clipboard_copy_command "true")"
4345
if tmux_is_at_least 2.4; then
4446
tmux bind-key -T copy-mode-vi "$(yank_key)" send-keys -X "$(yank_action)" "$copy_command"
4547
tmux bind-key -T copy-mode-vi "$(put_key)" send-keys -X copy-pipe-and-cancel "tmux paste-buffer"
4648
tmux bind-key -T copy-mode-vi "$(yank_put_key)" send-keys -X copy-pipe-and-cancel "$copy_command; tmux paste-buffer"
4749
tmux bind-key -T copy-mode-vi "$(yank_wo_newline_key)" send-keys -X "$(yank_action)" "$copy_wo_newline_command"
50+
if [[ "$(yank_with_mouse)" == "on" ]]; then
51+
tmux bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X "$(yank_action)" "$copy_command_mouse"
52+
fi
4853

4954
tmux bind-key -T copy-mode "$(yank_key)" send-keys -X "$(yank_action)" "$copy_command"
5055
tmux bind-key -T copy-mode "$(put_key)" send-keys -X copy-pipe-and-cancel "tmux paste-buffer"
5156
tmux bind-key -T copy-mode "$(yank_put_key)" send-keys -X copy-pipe-and-cancel "$copy_command; tmux paste-buffer"
5257
tmux bind-key -T copy-mode "$(yank_wo_newline_key)" send-keys -X "$(yank_action)" "$copy_wo_newline_command"
58+
if [[ "$(yank_with_mouse)" == "on" ]]; then
59+
tmux bind-key -T copy-mode MouseDragEnd1Pane send-keys -X "$(yank_action)" "$copy_command_mouse"
60+
fi
5361
else
5462
tmux bind-key -t vi-copy "$(yank_key)" copy-pipe "$copy_command"
5563
tmux bind-key -t vi-copy "$(put_key)" copy-pipe "tmux paste-buffer"
5664
tmux bind-key -t vi-copy "$(yank_put_key)" copy-pipe "$copy_command; tmux paste-buffer"
5765
tmux bind-key -t vi-copy "$(yank_wo_newline_key)" copy-pipe "$copy_wo_newline_command"
66+
if [[ "$(yank_with_mouse)" == "on" ]]; then
67+
tmux bind-key -t vi-copy MouseDragEnd1Pane copy-pipe "$copy_command_mouse"
68+
fi
5869

5970
tmux bind-key -t emacs-copy "$(yank_key)" copy-pipe "$copy_command"
6071
tmux bind-key -t emacs-copy "$(put_key)" copy-pipe "tmux paste-buffer"
6172
tmux bind-key -t emacs-copy "$(yank_put_key)" copy-pipe "$copy_command; tmux paste-buffer"
6273
tmux bind-key -t emacs-copy "$(yank_wo_newline_key)" copy-pipe "$copy_wo_newline_command"
74+
if [[ "$(yank_with_mouse)" == "on" ]]; then
75+
tmux bind-key -t emacs-copy MouseDragEnd1Pane copy-pipe "$copy_command_mouse"
76+
fi
6377
fi
6478
}
6579

0 commit comments

Comments
 (0)