Create a gist now

Instantly share code, notes, and snippets.

@wellle /yank.vim
Last active Jul 15, 2017

What would you like to do?
Yank without moving the cursor to the beginning of the yanked text
nnoremap <silent> y :<C-U>call MarkAndSetOpfunc()<CR>g@
vnoremap <silent> y :<C-U>call MarkYankAndJump()<CR>
function! MarkAndSetOpfunc()
let g:save_cursor = getpos(".")
set opfunc=YankAndJumpBack
function! MarkYankAndJump()
let g:save_cursor = getpos(".")
call YankAndJumpBack(visualmode(), 1)
function! YankAndJumpBack(type, ...)
if a:0
silent exe "normal! `<" . a:type . "`>y"
elseif a:type == 'line'
silent exe "normal! '[V']y"
elseif a:type == 'block'
silent exe "normal! `[\<C-V>`]y"
silent exe "normal! `[v`]y"
call setpos('.', g:save_cursor)

wellle commented Feb 1, 2014

TODO: Use v:register to make it work with registers.

jmlucjav commented Mar 6, 2014

added this to my .vimrc, mapped to leader-y, working perfectly, thanks!

jmlucjav commented Feb 5, 2015

I am cleaning up my vimrc, the n mapping works, but I am not sure what the v mapping is supposed to do???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment