How to augment vim yank and paste, so when I yank, vim writes the content to a file. When I paste, it uses the content from the file. I want to have a system wide file which serves as a global buffer.
Context: I run vim in different tmux splits on a remote server (over ssh). I want seamlessly copy and paste between vims in tmux splits. I tried a bunch of plugins, but none of them worked, thus the question.
You can use a movement command or up, down, right, and left arrow keys. Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.
If you're using Vim in visual mode, the standard cut and paste keys also apply, at least with Windows. CTRL A means "Mark the entire file. CTRL C means "Copy the selection.
I believe vim allows you to do it like this:
:wv
and hit enter:rv
and hit enterp
to paste the content of your (now) global clipboardThis should work across your different vim sessions independently of whether they run in different tmux panes or not.
You want to have a look at:
map-operator
because y
is an operator: a motion pending commandreadfile()
and writefile()
cause the r
or w
are commands, nice to use but not for scriptsvisualmode()
to get the selectionThe shared file was hardcoded /tmp/yank.txt
:
function! Yank(type, ...)
" Invoked from Visual mode, use gv command.
if a:0
silent exe "normal! gvy"
elseif a:type == 'line'
silent exe "normal! '[V']y"
else
silent exe "normal! `[v`]y"
endif
call writefile([@@], '/tmp/yank.txt')
endfunction
function! Paste(is_back)
let @t = join(readfile('/tmp/yank.txt'), '')
if a:is_back
normal! "tP
else
normal! "tp
endif
endfunction
nnoremap y :set opfunc=Yank<CR>g@
nmap Y Vy
vnoremap y :call Yank(visualmode(), 1)<CR>
vnoremap Y :call Yank(visualmode(), 1)<CR>
nnoremap p :call Paste(0)<CR>
nnoremap P :call Paste(1)<CR>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With