I have two files opened in vim, each in one tmux pane. I would like to copy let's say 10 lines from one file to another.
How can I accomplish this not using the mouse's
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
You'll have to use tmux shortcuts. Assuming your tmux command shortcut is the default: Ctrl+b, then:
Ctrl+b, [ Enter copy(?) mode.
Move to start/end of text to highlight.
Start highlighting text (on Arch Linux). When I've compiled tmux from source on OSX and other Linux's, just Space on its own usually works. Selected text changes the colours, so you'll know if the command worked.
Move to opposite end of text to copy.
Alt+w Copies selected text into
On Mac, use Esc+w. Try Enter if none of the above work.
Move cursor to opposite tmux pane, or completely different tmux window. Put the cursor where you want to paste the text you just copied.
Ctrl+b, ] Paste copied text from tmux clipboard.
tmux is quite good at mapping commands to custom keyboard shortcuts.
See Ctrl+b,? for the full list of set keyboard shortcuts.
If you are using vim and tmux on macOS (Tested on macOS 10.12.2, tmux 2.3):
(Assume that prefix key combination of tmux is prefix. The prefix is ctrl + b in defaults.)
I am connecting to my CentOS server on a Windows machine using ZOC terminal ssh client. Here is what works for me:
If you have
vim open, its better to use the vim copy paste to copy text.
Tmux copy paste will work, however there are several disadvantages.
First, when copying multiple lines of text, you will have to select multiple lines. Tmux copy paste typically doesnt understand that
vim has line numbers or there are markings in the vim gutter (such as relative numbers/absolute numbers). As a result, it will copy those as well.
Secondly, I have found that if the text has certain language specific formatting, and you directly try to copy paste using tmux to vim, it will mess up the formatting. This can be avoided by using
set paste!. However, this requires a lot more work.
"+yto copy and
Vim natively provides a method to copy paste between the vim buffer and the system clipboard, using either
"*y. Then pasting using
"*p. This will avoide copying the relative numbers in the gutter or other random marks which are not relevant to vim
I have chosen to use
Ctrl+Alt+V to copy and paste within tmux. Tmux's
copy-mode-vi enables navigation within a tmux pane with vim keys
hjkl^$..., rectangle mode is useful to copy table columns or when there are further splits within the pane, activate it with
Here is how I set it up in
bind P paste-buffer bind-key -T copy-mode-vi v send-keys -X begin-selection bind-key -T copy-mode-vi y send-keys -X copy-selection bind-key -T copy-mode-vi r send-keys -X rectangle-toggle # Also copy to system clipboard bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -sel clip -i' # map copy mode to ctrl+alt+c bind-key -n 'C-M-c' copy-mode # map paste buffer to ctrl+alt+v bind-key -n 'C-M-v' paste-buffer
I created this setup by modifying this blog post.
This copy-paste solution will also work if the 2 tmux panes (vim instances or anything else) are on different machines (one remote, one local).
A couple of options for vim use since the tmux shortcut sequence is bit long and I find it hard to remember
Like the answers above said you can use
"*y to copy and then
"*p respectively to paste. If you want vim to use the clipboard by default so you can just use
y/p directly then.
set clipboard=unamed "sets the default copy register to be * set clipboard=unamedplus "sets the default copy register to be +
source for clipboard command: https://vi.stackexchange.com/questions/84/how-can-i-copy-text-to-the-system-clipboard-from-vim
I've had the issue myself and uploaded my .vimrc and .tmux.conf files here: tvp-repl
You can configure tmux to
All you need to set up vim to copy to clipboard [visual mode Ctrl+c]:
set clipboard=unnamedplus vnoremap <C-c> "+y
Then have tmux command to run copying from vim and pasting the buffer to another pane [Ctrl+b Enter]:
bind-key Enter run "tmux send-keys -t 0 C-c" \; run "tmux select-pane -t 1" \; run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer" \; run "tmux send-keys -t 1 Enter" \; run "tmux select-pane -t 0"
Notice: a) This script requires xclip to interface with the clipboard. b) Assumption is vim is in pane 0; target pane is pane 1.