Skip to content

Commit

Permalink
tmux: tmux.conf: support mouse in wayland
Browse files Browse the repository at this point in the history
This adds the same behavior for the mouse bindings as when X is used.
It relies on an out of tree change on the tmux-yank plugin.

Signed-off-by: Nuno Sa <[email protected]>
  • Loading branch information
nunojsa committed Apr 18, 2024
1 parent d415797 commit 20d8541
Showing 1 changed file with 49 additions and 20 deletions.
69 changes: 49 additions & 20 deletions tmux/tmux.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,56 @@ set-option -g prefix C-x
bind-key C-x send-prefix

setw -g word-separators ' ,;()'
# This allows for middle mouse paste (from other apps) without having to press shift.
bind -n MouseDown2Pane run "tmux set-buffer -b primary_selection -- \"$(xsel -o)\"; \
tmux paste-buffer -b primary_selection; tmux delete-buffer -b primary_selection"

# With the above binding, the default double and triple click actions do not work as the middle
# button paste now looks for the system clipboard to paste from. As such we need the next bindings
# to fix that. The double click bindings will select the whole text until the next separator (as
# specified in word-separators) is found. Hence, we can double click anywhere in some word to select
# the full word.
bind -T copy-mode-vi DoubleClick1Pane select-pane \; send -X previous-word \; send -X select-word \; \
send -X copy-pipe-no-clear "xsel -i"
bind -n DoubleClick1Pane select-pane \; copy-mode \; send -X previous-word \; send -X select-word \; \
send -X copy-pipe-no-clear "xsel -i"
bind -T copy-mode-vi TripleClick1Pane select-pane \; send -X select-line \; send -X copy-pipe-no-clear "xsel -i"
bind -n TripleClick1Pane select-pane \; copy-mode \; send -X select-line \; run-shell -d 0.3 \; send -X copy-pipe-no-clear "xsel -i"
bind -n MouseDrag1Pane copy-mode -M
if-shell '[[ $(env | grep XDG_SESSION_TYPE | cut -d"=" -f2) = wayland ]]' {
set -g @yank_wayland_mouse 'true'
# This allows for middle mouse paste (from other apps) without having to press shift.
bind -n MouseDown2Pane run "tmux set-buffer -b primary_selection -- \"$(wl-paste --primary)\"; \
tmux paste-buffer -b primary_selection; tmux delete-buffer -b primary_selection"

# With the above binding, the default double and triple click actions do not work as the middle
# button paste now looks for the system clipboard to paste from. As such we need the next bindings
# to fix that. The double click bindings will select the whole text until the next separator (as
# specified in word-separators) is found. Hence, we can double click anywhere in some word to select
# the full word.
bind -T copy-mode-vi DoubleClick1Pane select-pane \; send -X previous-word \; send -X select-word \; \
send -X copy-pipe-no-clear "wl-copy --primary"
bind -n DoubleClick1Pane select-pane \; copy-mode \; send -X previous-word \; send -X select-word \; \
send -X copy-pipe-no-clear "wl-copy --primary"
bind -T copy-mode-vi TripleClick1Pane select-pane \; send -X select-line \; \
send -X copy-pipe-no-clear "wl-copy --primary"
bind -n TripleClick1Pane select-pane \; copy-mode \; send -X select-line \; run-shell -d 0.3 \; \
send -X copy-pipe-no-clear "wl-copy --primary"

# As @yank_action is not set to automatically exit copy-mode, use mouse righ-click to easily exit
# copy-mode when the mouse is being used. This works for me, as I don't really find the context menu
# useful. Might revisit this binding in the future though.
bind -T copy-mode-vi MouseDown3Pane select-pane\; copy-mode -H \; send -X copy-pipe-and-cancel "wl-copy"
} {
# This allows for middle mouse paste (from other apps) without having to press shift.
bind -n MouseDown2Pane run "tmux set-buffer -b primary_selection -- \"$(xsel -o)\"; \
tmux paste-buffer -b primary_selection; tmux delete-buffer -b primary_selection"

# With the above binding, the default double and triple click actions do not work as the middle
# button paste now looks for the system clipboard to paste from. As such we need the next bindings
# to fix that. The double click bindings will select the whole text until the next separator (as
# specified in word-separators) is found. Hence, we can double click anywhere in some word to select
# the full word.
bind -T copy-mode-vi DoubleClick1Pane select-pane \; send -X previous-word \; send -X select-word \; \
send -X copy-pipe-no-clear "xsel -i"
bind -n DoubleClick1Pane select-pane \; copy-mode \; send -X previous-word \; send -X select-word \; \
send -X copy-pipe-no-clear "xsel -i"
bind -T copy-mode-vi TripleClick1Pane select-pane \; send -X select-line \; send -X copy-pipe-no-clear "xsel -i"
bind -n TripleClick1Pane select-pane \; copy-mode \; send -X select-line \; run-shell -d 0.3 \; \
send -X copy-pipe-no-clear "xsel -i"

# As @yank_action is not set to automatically exit copy-mode, use mouse righ-click to easily exit
# copy-mode when the mouse is being used. This works for me, as I don't really find the context menu
# useful. Might revisit this binding in the future though.
bind -T copy-mode-vi MouseDown3Pane select-pane\; copy-mode -H \; \
send -X copy-pipe-and-cancel "xsel -i --clipboard"
}

# As @yank_action is not set to automatically exit copy-mode, use mouse righ-click to easily exit
# copy-mode when the mouse is being used. This works for me, as I don't really find the context menu
# useful. Might revisit this binding in the future though.
bind -T copy-mode-vi MouseDown3Pane select-pane\; copy-mode -H \; send -X copy-pipe-and-cancel "xsel -i --clipboard"
bind -n MouseDrag1Pane copy-mode -M
# use left mouse key to clear a selection
bind -T copy-mode-vi MouseDown1Pane select-pane\; send -X clear-selection

Expand Down

0 comments on commit 20d8541

Please sign in to comment.