1

Let's say I want to edit the text

blahblah WombatModeFunctor

to be

blahblah WombatModeFunctor::WombatModeFunctor

The way I know to do it is:

  1. Move the caret (cursor) until it is over the W
  2. Use yw to yank the word
  3. Move the caret 1 position to the left of the W
  4. Use p to paste the yanked word. Now you have:

    blahblah WombatModeFunctorWombatModeFunctor

but the caret is over the first 'r', so you have to

  1. Move the caret 1 position to the right of the r
  2. Enter INSERT mode
  3. Type ::

My questions are:

  • Why is step 3 necessary? It seems silly that a p right after a yw would cause a pasting after the first letter of the word you just yanked.
  • Why is step 5 necessary? It seems more intuitive that the caret would be after the final pasted character, not on top of it
  • Is there a more efficient vim incantation for this action?

2 Answers 2

4
  1. Step 3 is not necessary, you can use P instead of p.
  2. Step 5 is not necessary, you can use a instead of i to enter insert mode.

An alternative approach is this, assuming cursor is on the first W:

C<c-r>"::<c-r>"

Here C will delete the text from the cursor to the end of line, then enter insert mode and <c-r>" (from insert mode) will paste the content of the last used register.

3
  • Thanks. I would have expected p to behave as P and i to behave as a, since the behavior I wanted seems like the more-common need. (Indeed, I cannot think of any case where I would want the behavior of p.) May 13, 2017 at 7:24
  • 2
    Actually, these are minor but important differences. I use both i, a, p and P often. It is all about understanding the different commands and when to apply them. i and P acts in front of normal mode cursor, while a and p acts after. Both behaviours are useful in different scenarios. May 13, 2017 at 8:09
  • @RyanV.Bissell You'll probably want p if you want to paste after the end of the line...
    – 8bittree
    May 15, 2017 at 15:48
2

With caret at the beginning of the line: wy$Pa::

3
  • Very nice! It's clear that I should spend more time thinking about how to economize my vim incantations. May 13, 2017 at 15:03
  • 1
    If you want to be even more economic, you might consider mapping Y to y$. This provides a nice analog to D and is often useful. It's old functionality will still be available to you via yy (which normally duplicates Y).
    – Octaviour
    Jun 8, 2017 at 12:37
  • Totally agree @Octaviour, I use Y a lot. I also enjoy the extra eye-candy of haya14busa/vim-operator-flashy's plugin, makes yanking feel better.
    – rafi
    Jun 8, 2017 at 13:53

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.