Yank Without Jank¶

For all the great things Vim has to offer, it still has some inconsistencies with basic editors that I simply can't unlearn. One of these nasties is moving the cursor after a visual yank. Go ahead, try it: vipy . Where's your cursor? Where did you expect it to be located? When you're in a boring editor and do shift-down-down-down <Cmd-c> , where's your cursor? Where did you expect it to be located? This janky behaviour always throws me off for a moment, then I compose myself, do a <backtick><greaterthan> to jump to the end of my selection, and p .

There must be a better way!

Solution A¶

Rebind y to do exactly what we did above:

vnoremap y y ` >

This work and I lived with it for a few minutes, but it still wasn't perfect. I noticed when I do line select using capital V the cursor would still move. The vertical motion was perfect, but horizontal motion was still jarring.

Solution B¶

Let's try using marks to keep things in place:

vnoremap y myy` y vnoremap Y myY` y

Y

Like a well trained dog, the cursor stays even though you yank it.

BONUS This snippet also takes over the y marker, so you can manually <backtick>y at a later time to continue yanking where you left off. This is great when you're moving a lot of stuff around and want pick up where you last were. You can also change the mark to capitals in the binding so it spans buffers, too.

I've been using this setting for a while and noticed my blood pressure is way down. No more yank anxiety means I'm a step closer to editing utopia!

Let me know how this goes for you in the comments below.

Commenter @Krzysztof noticed Solution B wasn't allowing the user to specify the target register. He was awesome enough to update the solution. Here's his solution:

vnoremap < expr > y "my\"" . v : register . "y`y"