Copy / paste text selections between tmux and the clipboard

22,873

Solution 1

Use the following tmux.conf with copy-pipe in the new versions of tmux (1.8+):

set -g mouse on
# To copy:
bind-key -n -t emacs-copy M-w copy-pipe "xclip -i -sel p -f | xclip -i -sel c "

# To paste:
bind-key -n C-y run "xclip -o | tmux load-buffer - ; tmux paste-buffer"
  1. prefix+[ into copy-mode
  2. select content with mouse(hold)
  3. M-w to copy that part into system clipboard
  4. C-y the paste it inside tmux, C-v to paste it inside other regular application like web browser.

Solution 2

Please note that, with Tmux 2.4 (since this commit), the binding syntax has changed. I paraphrase this Github comment to summarise the change briefly:

  1. replace -t with -T
  2. replace vi-<name> with <name>-mode-vi
  3. prefix the command with send-keys -X

I had:

bind-key -n -t vi-copy Enter copy-pipe 'xclip -i -sel p -f | xclip -i -sel c'
bind-key -n -t vi-copy MouseDragEnd1Pane copy-pipe 'xclip -i -sel p -f | xclip -i -sel c'

which I needed to change to:

bind-key -n -T copy-mode-vi Enter send-keys -X copy-pipe 'xclip -i -sel p -f | xclip -i -sel c'
bind-key -n -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe 'xclip -i -sel p -f | xclip -i -sel c'

From the changelog:

Key tables have undergone major changes. Mode key tables are no longer separate from the main key tables. All mode key tables have been removed, together with the -t flag to bind-key and unbind-key.

The emacs-edit, vi-edit, emacs-choose and vi-choose tables have been replaced by fixed key bindings in the command prompt and choose modes. The mode-keys and status-keys options remain.

The emacs-copy and vi-copy tables have been replaced by the copy-mode and copy-mode-vi tables. Commands are sent using the -X and -N flags to send-keys. So the following:

bind -temacs-copy C-Up scroll-up
bind -temacs-copy -R5 WheelUpPane scroll-up

Becomes:

bind -Tcopy-mode C-Up send -X scroll-up
bind -Tcopy-mode WheelUpPane send -N5 -X scroll-up

These changes allows the full command parser (including command sequences) and command set to be used - for example, the normal command prompt with editing and history is now used for searching, jumping, and so on instead of a custom one. The default C-r binding is now:

bind -Tcopy-mode C-r command-prompt -p'search up' "send -X search-backward-incremental '%%'"

There are also some new commmands available with send -X, such as copy-pipe-and-cancel.

Solution 3

While other answers may help, here's how I do the same:

  • Shift + select the text with the cursor.
  • Ctrl + Shift + C to copy the text to clipboard.

Solution 4

The -temacs-copy is not a typo. The leading dash means that this is an option to the bind-keys command (along with an argument for the option). It is equivalent to -t emacs-copy, if you like that better.

Copy mode uses a different set of bindings and commands1 than the “normal mode”. The copy-pipe command is only available in the alternate “mode” bindings tables. When mode-keys is emacs, copy mode will use the emacs-copy table, so you need to bind M-w to copy-pipe in the that table.

bind-key -temacs-copy M-w copy-pipe "xclip -i -selection clipboard"

Be careful making too many “no prefix” bindings. They will make it difficult to type those bound keys to a program running inside tmux (e.g. when you want to type C-y to a tty-mode instance of Emacs running inside tmux). You can always (prefix) bind some key that does send-keys C-y, but that might make it more cumbersome to type if you need it often enough.


1 Only movement and editing commands are available. With the exception of copy-pipe, these commands do not take any arguments (this is what the man page means with it says “One command in accepts an argument”; the “in” is probably a typo or an editing mistake).

Share:
22,873

Related videos on Youtube

Amelio Vazquez-Reina
Author by

Amelio Vazquez-Reina

I'm passionate about people, technology and research. Some of my favorite quotes: "Far better an approximate answer to the right question than an exact answer to the wrong question" -- J. Tukey, 1962. "Your title makes you a manager, your people make you a leader" -- Donna Dubinsky, quoted in "Trillion Dollar Coach", 2019.

Updated on September 18, 2022

Comments

  • Amelio Vazquez-Reina
    Amelio Vazquez-Reina over 1 year

    I am running the latest version of tmux (from the git repository) and xclip (0.12), and I would like to be able to use Emacs-like keyboard bindings to move around the text in copy-mode, copy (M-w) selections to the clipboard, and paste (C-y) from/to the copy buffer to the clipboard.

    So far I have been able to paste text with C-y, and move around in copy-mode with Emacs-like keyboard bindings, but I am still unable to copy text from a tmux buffer (e.g. in copy-mode)

    I found this thread for copying the entire buffer to the clipboard (and viceversa), but it does not seem to be working for me.

    Also, in the tmux-users mail list I was told that recent versions of tmux (only in the git repo) provide a command called copy-pipe. The man page says the following about this command:

    One command in accepts an argument, copy-pipe, which copies the selection and pipes it to a command. For example the following will bind ‘C-q’ to copy the selection into /tmp as well as the paste buffer:

           bind-key -temacs-copy C-q copy-pipe "cat >/tmp/out"
    

    It looks like copy-pipe is meant to be used in part to pipe the selection to another command. There also seem to be some typos in this description and in the command (what is temacs-copy?)

    Either way, what I would like to do is:

    Copying:

    1. Enter copy mode
    2. Move to the text I want to copy using Emacs navigation commands (i.e. C-f,C-b, M-f, M-b, C-a, C-e etc. to move the cursor). No prefix for any of these.
    3. Copy the selected text into the clipboard with: M-w (no prefix either)

    Pasting:

    1. I would like to be able to type C-y (without having to enter copy-mode) to paste text in the terminal (no prefix either)

    I have tried the following for copying without luck:

    bind-key -n M-w run "tmux save-buffer - | xclip -i -selection clipboard" 
    

    However, pasting works great:

    bind-key -n C-y run "xclip -o | tmux load-buffer - ; tmux paste-buffer"
    

    The odd thing is that I know that the "xclip -i -selection clipboard" part of the copy command above works well, since I can copy things to the clipboard in the command line, e.g.:

    echo "Hello world. How are you?" | xclip -i -selection clipboard
    

    With all this, how can I copy a selection from copy mode to the clipboard?

  • Louis Kottmann
    Louis Kottmann about 10 years
    This answer deserves more upvotes, it's the best for Linux-based setups.
  • roign
    roign about 7 years
    In tmux 2.0 it's run-shell instead of run
  • schaiba
    schaiba almost 7 years
    @LouisKottmann : why just Linux-based?
  • Louis Kottmann
    Louis Kottmann almost 7 years
    I cannot comment on other systems, but I believe mac uses bpaste instead of xclip usually
  • azzamsa
    azzamsa over 5 years
    In tmux 2.8 I had to change them into bind -Tcopy-mode M-w send-keys -X copy-pipe "xclip -i -sel p -f | xclip -i -sel c " . bind-key -n C-y run-shell "xclip -o | tmux load-buffer - ; tmux paste-buffer" according to this commit suggested by starfry
  • cjauvin
    cjauvin over 4 years
    This is a fair answer but the problems begin when you have two or more panes arranged horizontally.
  • Rishabh Agrahari
    Rishabh Agrahari over 4 years
    I see, let me know if you find something useful for that.
  • errolflynn
    errolflynn almost 4 years
    This is the simplest and most consistent solution I've found to copy and paste between tmux and firefox. I've been searching for something like this for 3 years.
  • sk29910
    sk29910 over 3 years
    I spent a long time trying to figure out why this didn't work for me, until I realized xclip didn't exist on my machine. Apparently it's not always installed by default. Hopefully this will save someone some time.