68

Example: copy in one tmux pane (via vim), then switch to another pane (running another vim instance) and paste using the vim paste command. I know this can be done via tmux (using prefix+]) but it would be really handy if I can copy and paste using vim bindings since i'm just switching between different panes running vim.

Any ideas?

gylaz
  • 11,981
  • 6
  • 48
  • 58
  • 1
    are the tmux panes running vim on different hosts? If not I'd just run a single instance of vim and use vim buffers to open the 2 files and paste between buffers. – sashang Jun 15 '12 at 00:41
  • this may be an issue with your version of vim not being compile with clipboard support - see @DmitrySemenov's answer below – JonnyRaa Dec 11 '18 at 16:43

8 Answers8

48

Sorry, I'm trying to convince you to use vim built-in features.


To make the copy/paste easy, you can open files in another Tabpages:

:tabe /path/to/another/file

Use gt or gT to switch Tabpages.


Or split the window to edit another file:

:sp /path/to/another/file

Use Ctrl-ww to switch Windows.
To split the window vertically, please use :vsp file


Update:

This is my .tmux.conf file:

# vim
setw -g mode-keys vi
bind [ copy-mode
bind -t vi-copy v begin-selection
bind -t vi-copy y copy-selection
bind -t vi-copy V rectangle-toggle
bind ] paste-buffer

# buffer
bind Space choose-buffer

I only use them when I need to copy terminal output.

kev
  • 137,128
  • 36
  • 241
  • 259
  • or rotate buffers, kev what about copying to the clipboard? – Eric Fortis Jun 15 '12 at 00:05
  • 1
    Is there no way to do it directly via tmux? I have different panes open in different directories and sometimes it's not convenient to :tabe or :sp and type out the long path. Much easier to switch the panes and paste. – gylaz Jun 18 '12 at 23:01
  • I leave the `prefix-[,]` as default. Because I have learned them by heart. You can config them to what you want. – kev Jun 18 '12 at 23:23
  • This didn't work for me. It hit prefix [, then v, then I move to another line, then y, then prefix ], but nothing happens, even after I restart tmux. – Rose Perrone Mar 12 '13 at 01:37
  • 1
    I think there are reasons to not just use a single vim instance. For instance, you might want only three panes open in your terminal: one half-screen editing session, a fourth-screen editing session, and a fourth-screen terminal. I'm not sure this can be accomplished with one instance of vim. – Zach Conn Jun 26 '13 at 13:35
  • There are legitimate reasons to use several Vim instances. For example, what if I have two monitors, but I want to edit files on both of them? Or what if I have several tmux sessions for completely different projects, but I want to copy some text from a file in one project to a file in another? Being stuck with a single, system-wide Vim instance is very inflexible. Vim is a text editor, not an operating system. – Will Vousden Mar 03 '16 at 10:07
  • 2
    For newer versions of tmux, it is `bind-key -T copy-mode-vi ...` instead of `bind-key -t vi-copy ...` – chrimuelle Mar 01 '18 at 12:49
22

I've been used this handy binding for several years :)

" copy to buffer
vmap <C-c> :w! ~/.vimbuffer<CR>
nmap <C-c> :.w! ~/.vimbuffer<CR>
" paste from buffer
map <C-p> :r ~/.vimbuffer<CR>
chenkaie
  • 441
  • 2
  • 7
  • 3
    Exactly, it didn't copy to system clipboard. But it fulfill the requirement of this issue then :) – chenkaie Nov 05 '15 at 06:26
14

I'm currently stuck with a laptop running Windows at work, but I do all my development on a server running Linux, so I end up working in a tmux session over PuTTY all day.

I wanted to copy text between vim instances running in different windows in my tmux session. I tried using the * register to copy to the system clipboard, but since I'm connected through PuTTY I don't have an X session, so there is no system clipboard, even if I launch gVim instead of vim. (I might have been able to use x-forwarding or something to fix this, but I didn't want to install an x-server on windows.)

I thought that there should be a way to use the tmux copy/paste buffer from vim in place of the system keyboard, and sure enough someone has written a plugin for that.

Installing fakeclip adds a new register, &, which maps to tmux's paste buffer. Since all my vim instances are within the same tmux session, this makes it super simple to yank/put text between them.

The plugin documentation says that fakeclip should also work with gnu screen, but I haven't personally tested that.


tl;dr

Install the fakeclip plugin, and then you can use "&y to yank into tmux's buffer, and "&p to put from tmux's buffer.

tshepang
  • 10,772
  • 21
  • 84
  • 127
DaoWen
  • 31,184
  • 6
  • 65
  • 95
12

Although I agree that it's better to just use one vim instance, you can do this with tmux alone. It has a built in copy-mode. My tmux.conf is set up like:

  setw -g mode-keys vi
  unbind [
  unbind p
  bind C-y copy-mode
  bind p paste-buffer
  bind -t vi-copy v begin-selection
  bind -t vi-copy y copy-selection
  bind -t vi-copy Escape cancel

So you can use prefix-<C-y> to activate copy mode, /search term as an example to go where you want, v to visually select, y to yank into tmux. Then go to other vim session and get into insert mode. Use prefix-p to paste what's in the tmux paste buffer. There are also ways to copy tmux's paste buffer to your system clipboard.

Conner
  • 27,462
  • 8
  • 47
  • 72
  • 1
    Why is it better to use one vim instance? Surely this is incompatible with the idea of having different workspaces? It doesn't seem very practical when I have 5 large, unrelated projects to work on. – Will Vousden May 23 '17 at 08:44
  • Mostly a matter of opinion and/or CPU efficiency. Here's a video to watch: https://vimeo.com/4446112 – Conner May 24 '17 at 00:35
3

You can copy the content to clipboard using vim bindings, then switch to the pane and paste it. The following key-bindings might be handy.

noremap ty "+y
noremap tY "+Y  // copy a whole line to the clipboard
noremap tp "+p  // put the text from clipboard after the cursor 
noremap tP "+P  // put the text from clipboard before the cursor 

The "+ register is used to access system's clipborad in Vim.

Some useful tips:
1. You can press v or V or ctrl-v to enter visual mode, then select the content you want to yank, and press ty to copy it to your clipboard.
2. ty can be combined with other motions. For instance, tyaw can be used to copy a word under the cursor to the clipboard. tyib copy the content inside parentheses. tyi" copy the text inside double quotation, etc...

See also How to make vim paste from (and copy to) system's clipboard? for further explanation.

Community
  • 1
  • 1
knight42
  • 840
  • 10
  • 14
  • For the sake of anyone who may not know those commands, could you add an explanation of each one? It would make the answer more useful! –  May 27 '15 at 18:10
  • @PetahChristian I am quite surprised that my answer should be noticed! I expand my answer now. If there is something ambiguous or grammar mistakes(I 'm not a native English speaker), please let me know :) – knight42 May 30 '15 at 03:15
2

solution on Fedora24

you need to use vimx instead of vim

put in your .bashrc or .zshrc

alias vi='vimx'
alias vim='vimx'

and then you can easily copy between vims/tmux panels


$ vim --version | grep clipboard
-clipboard       +insert_expand   +path_extra      -toolbar
+eval            +mouse_dec       +statusline      -xterm_clipboard

$ vimx --version | grep clipboard
+clipboard       +insert_expand   +path_extra      +toolbar
+eval            +mouse_dec       +statusline      +xterm_clipboard

vimx is part of vim-enhanced-2:7.4.1868-1.fc24.x86_64 you most likely have installed

DmitrySemenov
  • 6,544
  • 10
  • 41
  • 64
2

This works in neovim.

Execute :checkhealth, you will find something like these:

## Clipboard (optional)
  - OK: Clipboard tool found: tmux

If not, maybe you need to upgrade the tmux to the latest version 2.9.

Then add the following code in '~/config/nvim/init.vim'

set clipboard+=unnamedplus
Anthony Zhan
  • 763
  • 7
  • 11
0

control+insert = copy

shift+insert = paste