You want to have a look at:
map-operator
because y
is an operator: a motion pending command
readfile()
and writefile()
cause the r
or w
are commands, nice to use but not for scripts
visualmode()
to get the selection
The 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>