Skip to content

Instantly share code, notes, and snippets.

@wellle
Last active May 10, 2023 02:36
Show Gist options
  • Select an option

  • Save wellle/8633440 to your computer and use it in GitHub Desktop.

Select an option

Save wellle/8633440 to your computer and use it in GitHub Desktop.
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
endfunction
function! MarkYankAndJump()
let g:save_cursor = getpos(".")
call YankAndJumpBack(visualmode(), 1)
endfunction
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"
else
silent exe "normal! `[v`]y"
endif
call setpos('.', g:save_cursor)
endfunction
@wellle

wellle commented Feb 1, 2014

Copy link
Copy Markdown
Author

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

@jmlucjav

jmlucjav commented Mar 6, 2014

Copy link
Copy Markdown

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

@jmlucjav

jmlucjav commented Feb 5, 2015

Copy link
Copy Markdown

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

@chenzhihuai1990

chenzhihuai1990 commented Feb 26, 2019

Copy link
Copy Markdown

It seem dose not work for command yy and y[num]y.

@astier

astier commented Mar 3, 2023

Copy link
Copy Markdown

nnoremap yy yy fixed yy for me.

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