Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System Clipboard Vim within TMUX within SSH session

Tags:

vim

ssh

tmux

I have vim open inside tmux inside an ssh session. How can I make vim use my laptop's system clipboard as the default copy paste? The default set clipboard=unamed isn't working. Both systems are Ubuntu in case that matters.

like image 367
Sauhaarda Chowdhuri Avatar asked Jul 21 '17 23:07

Sauhaarda Chowdhuri


People also ask

How do I set the clipboard in tmux?

In newer versions of tmux, you can tell tmux to handle interactions with the clipboard for you. See set-clipboard tmux option. on — tmux will create an inner buffer and attempt to set the terminal clipboard using OSC 52. external — do not create a buffer, but still attempt to set the terminal clipboard.

Can I use pbcopy or xclip with tmux on remote machines?

So far we covered only local scenarios. When you SSH to remote machine, and start tmux sessions there, you cannot make use of pbcopy, xclip or xsel, because text will be stored in the remote machine’s clipboard, not in your local one. You need some way to transport copied text to your local machine’s clipboard.

How to copy text while in copy mode in tmux?

The idea is to hook into various tmux commands, that manage to copy text while in copy mode. copy-selection-and-cancel and copy-end-of-line are special tmux commands which tmux understand when pane is in copy mode. There are two flavors of copy command: copy-selection and copy-pipe.

How do I open the clipboard in Linux terminal?

If you’re using iTerm, try to execute following command, and then “ ⌘V ” to see contents of system clipboard. Make sure to turn on OSC 52 escape sequence handling: “Preferences -> General -> Applications in terminal may access clipboard”.


2 Answers

Clipboard integration feature (PASTE64/OSC52) is helpful if your terminal emulator supports it. For example, iTerm2 supports it (I am not sure about Ubuntu).

Add this function to your "remote" .vimrc. yank something and run :OscCopy. It works even it is inside tmux session.

function! OscCopy()
  let encodedText=@"
  let encodedText=substitute(encodedText, '\', '\\\\', "g")
  let encodedText=substitute(encodedText, "'", "'\\\\''", "g")
  let executeCmd="echo -n '".encodedText."' | base64 | tr -d '\\n'"
  let encodedText=system(executeCmd)
  if $TMUX != ""
    "tmux
    let executeCmd='echo -en "\x1bPtmux;\x1b\x1b]52;;'.encodedText.'\x1b\x1b\\\\\x1b\\" > /dev/tty'
  else
    let executeCmd='echo -en "\x1b]52;;'.encodedText.'\x1b\\" > /dev/tty'
  endif
  call system(executeCmd)
  redraw!
endfunction
command! OscCopy :call OscCopy()

gist

like image 136
Gre-san Avatar answered Oct 22 '22 13:10

Gre-san


You need to do two things.

  1. On your remote system, install a clipboard-aware Vim (and the X dependencies needed for clipboard support):

    $ sudo apt-get install vim-gtk
    
  2. On your local system, start your ssh session with X11 forwarding enabled:

    $ ssh -X user@hostname
    

    See $ man ssh for the security implications of X11 forwarding.

like image 32
romainl Avatar answered Oct 22 '22 12:10

romainl