Search code examples
terminalgnu-screentmux

Getting back old copy paste behaviour in tmux, with mouse


This is what I used to do in tmux to copy-paste (using the mouse, the keyboard works differently and it is not what I am interested about):

  1. Select text with mouse, left-button pressed
  2. Paste text with middle-button

I have upgraded my OS, and this has gotten a new tmux version. I have not changed my .tmux.conf config file.

This is what I have to do with the current version of tmux, 1.6 (which comes pre-packaged in the latest crunchbang linux):

  1. Select text with mouse, left-button pressed and shift key
  2. Paste text with middle-button
  3. Terminal gets blocked, a litte information area shows some numbers on the top right of the current pane (i.e. [0/24], probably something related to how many characters have been pasted), which mean little to me and I do not need / want (edit: it seems copy-mode is entered automatically here)
  4. I have to press the q key to get a functional terminal again.

This is too much hassle for something I do dozens of times a day. How to get the old mechanism working again?


Solution

  • To restore the default copy/paste configuration you need to (at least temporarily) turn off mouse support within tmux:

    prefix : set -g mouse off
    

    Where prefix is the tmux access key (Ctrl+B by default unless you re-map it). : starts command mode and set -g sets the parameter globally.

    When mouse mode is turned off, the standard copy/paste functions provided by your operating system work as expected.

    Something else you might want to do is 'maximise' the current pane, so you can copy multiple lines easily. Ctrl+B, z toggles the 'zoom' of the current pane.

    The code below can be added to your .tmux.conf file to bind the m and M keys to 'mouse on' and 'mouse off' respectively. This lets you use Ctrl+B, m to turn on mouse support; Ctrl+B, M to turn it off.

    # toggle mouse mode to allow mouse copy/paste
    # set mouse on with prefix m
    bind m \
        set -g mouse on \;\
        display 'Mouse: ON'
    # set mouse off with prefix M
    bind M \
        set -g mouse off \;\
        display 'Mouse: OFF'                                                                       
    

    If you’re working with an old (pre-2.1) version of tmux, you instead need to use the following:

    prefix : set -g mode-mouse off
    

    There are more details and some handy key bindings to automate all this here:

    http://tangledhelix.com/blog/2012/07/16/tmux-and-mouse-mode/

    The main thrust of the article linked to above is this excerpt from .tmux.conf:

    # disable mouse control by default - change 'off' to 'on' to enable by default.
    setw -g mode-mouse off
    set-option -g mouse-resize-pane off
    set-option -g mouse-select-pane off
    set-option -g mouse-select-window off
    # toggle mouse mode to allow mouse copy/paste
    # set mouse on with prefix m
    bind m \
        set -g mode-mouse on \;\
        set -g mouse-resize-pane on \;\
        set -g mouse-select-pane on \;\
        set -g mouse-select-window on \;\
        display 'Mouse: ON'
    # set mouse off with prefix M
    bind M \
        set -g mode-mouse off \;\
        set -g mouse-resize-pane off \;\
        set -g mouse-select-pane off \;\
        set -g mouse-select-window off \;\
        display 'Mouse: OFF'
    # zoom this pane to full screen
    bind + \
        new-window -d -n tmux-zoom 'clear && echo TMUX ZOOM && read' \;\
        swap-pane -s tmux-zoom.0 \;\
        select-window -t tmux-zoom
    # restore this pane
    bind - \
        last-window \;\
        swap-pane -s tmux-zoom.0 \;\
        kill-window -t tmux-zoom