7

I frequently find myself wanting to make a copy of, eg, a function that I just wrote and modify it slightly. In such a situation, I'll generally use something like y5k to copy what I just wrote. I then have to 5j or `` to get back to where I just was before p.

However, if I'm at the start of what I want to copy, y5j will not move the cursor to the end of what I just yanked, meaning that I still have to 5j before I p. This behavior is mentioned briefly in the help files:

After applying the operator the cursor is mostly left at the start of the text that was operated upon. For example, "yfe" doesn't move the cursor, but "yFe" moves the cursor leftwards to the "e" where the yank started.

But it doesn't give a rationale for which motions move the cursor and which don't, or a way to change these behaviors.

So, long story short: how can I yank upwards (or in any particular direction) without moving the cursor?

2 Answers 2

8

You can take a different approach for this exact situation.

Paste differently

The gP command pastes before your cursor like P, but leaves your cursor to the end of the pasted text instead of before.

This is effectively identical to manually restoring your cursor position before pasting with p.

1
  • 1
    That.... is even better. It's more succinct, and somehow feels more Vim-y. Thanks!
    – Tom
    Commented Mar 12, 2015 at 22:37
5

Every command that modified the buffer (and yanks) will set the '[ and '] marks around the changed area. So you can go to the end of the the freshly yanked text via '] and then do your put, p.

If you are just yanking line-winse then you can use the :yank command with a range.

:.-5,.yank

This command will yank the 5 lines above the current line to the current line. Or even shorter:

:-5,y

Details:

  • Current line is represented by .
  • .-5 means subtract 5 from the current line
  • The current line can be assumed so leave it out
  • :y is short for :yank

For more help see:

:h :y
:h range
3
  • That is both good to know and an improvement, but still not ideal. What I'm really looking for is a succinct way to yank upward without having to move the cursor back to where it was.
    – Tom
    Commented Mar 12, 2015 at 22:04
  • Thanks, your edit is pretty much what I'm looking for. Your first paragraph, however, doesn't seem to work as you describe: ] does not move to the end of what I yank.
    – Tom
    Commented Mar 12, 2015 at 22:13
  • He said '] not ]. Commented Mar 12, 2015 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.