39

I am learning to use tmux, I found when I in a tmux window, double-click to select and copy function did not work any more.

Can I use double-click to select and copy just as in iterm2?

I have googled for some time, but did not find an short and clear answer to this. I have added setw -g mode-mouse on in the tmux configure file already.

ideasman42
  • 42,413
  • 44
  • 197
  • 320
WKPlus
  • 6,955
  • 2
  • 35
  • 53

8 Answers8

67

I found a way to achieve that: hold the option key when double clicking.

WKPlus
  • 6,955
  • 2
  • 35
  • 53
45

Don't know about iterm2, but this can be made to work in tmux 3.0 or newer
(tested on Linux w/ tmux 3.0, last command uses X11 xclip).

Added triple click to select and copy a line too.

# Double LMB Select & Copy (Word)
bind-key -T copy-mode-vi DoubleClick1Pane \
    select-pane \; \
    send-keys -X select-word-no-clear \; \
    send-keys -X copy-pipe-no-clear "xclip -in -sel primary"
bind-key -n DoubleClick1Pane \
    select-pane \; \
    copy-mode -M \; \
    send-keys -X select-word \; \
    send-keys -X copy-pipe-no-clear "xclip -in -sel primary"

# Triple LMB Select & Copy (Line)
bind-key -T copy-mode-vi TripleClick1Pane \
    select-pane \; \
    send-keys -X select-line \; \
    send-keys -X copy-pipe-no-clear "xclip -in -sel primary"
bind-key -n TripleClick1Pane \
    select-pane \; \
    copy-mode -M \; \
    send-keys -X select-line \; \
    send-keys -X copy-pipe-no-clear "xclip -in -sel primary"

If you don't use copy-mode-vi, replace this with copy-mode.


For older tmux versions check the edit-history.

ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • 2
    I've been waiting for this feature for so long. So glad you pointed it out! – ivan Nov 03 '17 at 02:59
  • Note that this requires tmux 2.4+ – ssmith Dec 20 '17 at 15:41
  • 1
    Is `select-pane` necessary in this context? I would assume that `DoubleClick1Pane` does it implicitly (not sure) – cjauvin Mar 27 '18 at 14:56
  • This does not work without holding the shift key and if you choose to hold the shift key then double click to select a word and triple click works anyways with mouse mode on, and does not require these lines. I was expecting that these lines would allow me to select a word and line without holding the Shift key when i am in mouse mode. I am using tmux 2.7. – Rohit Aug 22 '18 at 14:35
  • Actually it works, but it does not highlight the word or line, so i didn't realize it in first go. Also can you please explain the commands above, so that we can understand what each line is doing – Rohit Aug 22 '18 at 14:43
  • Using tmux 2.7x also, this is selecting the work on double click, line on triple click. – ideasman42 Aug 23 '18 at 04:04
  • Wonder why it is not the default behavior (without the `xclip` part perhaps)? Almost all terminals work like this. Thank you @ideasman42. – Nishant Jan 26 '19 at 05:56
15

In Alacrity holding Shift allows copying as if there's no tmux.

source

seeker_of_bacon
  • 1,939
  • 2
  • 19
  • 20
5

Building off of @ideasman42 's answer. This is using tmux 2.8 and pbcopy for macos mojave.

# Double LMB Select & Copy (Word)
bind-key -n DoubleClick1Pane \
    select-pane \; \
    copy-mode -M \; \
    send-keys -X select-word \; \
    run-shell "sleep .5s" \; \
    send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -n DoubleClick1Pane \
    select-pane \; \
    copy-mode -M \; \
    send-keys -X select-word \; \
    run-shell "sleep .5s" \;
    send-keys -X copy-pipe-and-cancel "pbcopy

My version selects the word, briefly highlights it, copies it to the system buffer and then cancels copy-mode.

pete-may
  • 59
  • 1
  • 3
  • 2
    That looks like the same command written twice, is the second one supposed to be `TripleClick1Pane`? Either way, this works great in 2.8! Had to delete the second one though, tmux didn't like the re-binding for some reason. – sevko May 06 '20 at 00:18
4

I have figured out a copy paste mechanism that is similar of what you will expect form a terminal

I used the following settings to be able to:

  1. select a word with a mouse double click action
  2. select a line with a mouse tripple click action
  3. select a partial line a mouse drag and drop action

This solution will keep the selection highlighted and copy the selection output to both clipboard buffers (primary and clipboard)

When you hit "Enter" you exit and go back to the shell

The advantage here is that you can use both middle mouse button as shift-insert combination outside of tmux to paste the content, while it is still selected.

Also when you exited back to the shell, you can use middle mouse button or hit shift-insert to paste the content

All what you would expect from a normal terminal environment

    # Enable mouse control
    setw -g mouse on

    unbind -T copy-mode-vi Enter
    bind-key -T copy-mode-vi Enter \
        send -X cancel

    # Drag and Drop Aelect & Copy (Selection)
    bind-key -T copy-mode-vi MouseDragEnd1Pane \
        send-keys -X copy-pipe "xclip -in -f | xclip -in -sel c" \; \
        send-keys -X no-clear

    # Double LMB Select & Copy (Word)
    bind-key -T copy-mode-vi DoubleClick1Pane \
        select-pane \; \
        send-keys -X select-word \; \
        send-keys -X copy-pipe "xclip -in -f | xclip -in -sel c" \; \
        send-keys -X no-clear
    bind-key -n DoubleClick1Pane \
        select-pane \; \
        copy-mode -M \; \
        send-keys -X select-word \; \
        send-keys -X copy-pipe "xclip -in -f | xclip -in -sel c" \; \
        send-keys -X no-clear

    # Triple LMB Select & Copy (Line)
    bind-key -T copy-mode-vi TripleClick1Pane \
        select-pane \; \
        send-keys -X select-line \; \
        send-keys -X copy-pipe "xclip -in -f | xclip -in -sel c" \; \
        send-keys -X no-clear
    bind-key -n TripleClick1Pane \
        select-pane \; \
        copy-mode -M \; \
        send-keys -X select-line \; \
        send-keys -X copy-pipe "xclip -in -f | xclip -in -sel c" \; \
        send-keys -X no-clear

    # Middle click to paste from the primary buffer
    unbind-key MouseDown2Pane
    bind-key -n MouseDown2Pane run "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer"

    # Shift insert to paste from the clipboard
    unbind-key S-IC
    bind-key S-IC run "tmux set-buffer \"$(xclip -o -sel c)\"; tmux paste-buffer"
  • NOTE1 : in order for this to work across a ssh session : -X has to be provided as option to ssh
  • NOTE2: I'm using tmux version 2.8
Filip Van Genck
  • 181
  • 2
  • 3
2

On Kitty/Alacritty, we double-click on the text-block while keeping Shift pressed. And copying works fine natively as well as within tmux.

Rounak Datta
  • 442
  • 7
  • 10
1

This is using tmux 3.3 and pbcopy for macOS Ventula. this works for me.

# Double LMB Select & Copy (Word)
bind-key -T root DoubleClick1Pane select-pane \; copy-mode \; send-keys -MX select-word \; run-shel\
    l "sleep 0.1" \; send-keys -X copy-pipe-and-cancel "pbcopy"                                         

# Triple LMB Select & Copy (Line)
bind-key -T root TripleClick1Pane select-pane \; copy-mode \; send-keys -MX select-line \; run-shel\
    l "sleep 0.1" \; send-keys -X copy-pipe-and-cancel "pbcopy"  
0

Just uncheck the "Enable mouse reporting" option in iTerm2.

enter image description here

But this has side effect: set -g mouse on in ~/.tmux.conf will not work.

LiaoL
  • 121
  • 1
  • 8