8

If I have a pair of quotes on a line by themselves:

''

...and a short paragraph of text, say three lines of text:

some text
that I'd like 
to move

Now if I want to move the text into the quotes, I can place the caret at the line above the first line of text and press 3, dd to select and delete them - but how can I paste them in between the quotes in the easiest way possible? The problem is that pasting them will add them on the line below the quotes, producing this result:

''
some text
that I'd like 
to move

Until now I've typically done something like entering a newline between the quotes, then pasting the text between them, resulting in something like this...

'
some text
that I'd like 
to move
'

...after which I'd remove the newlines at the top and bottom using shift+j. This works, but it feels clumsy.

Is there a better way?

Just to be clear, this is the result I'm trying to achieve:

'some text
that I'd like 
to move'
1
  • My initial thought was to use :help o_v for this, but when I tried to formulate a solution I kept getting thwarted by the exceptions described in :help exclusive. I'd be interested to know if there are any solutions using o_v that I just missed.
    – Rich
    Commented Oct 18, 2019 at 10:31

2 Answers 2

9

When you paste the contents of a register in insert mode, you don't have the initial newline.

For instance, we have this text:

''
some text
that I'd like
to move

Place your caret on the beginning of the second line in normal mode and type 3ddaCtrl+R"Backspace:

  • 3dd: cut the three following lines;
  • a: go to insert mode after the selected character;
  • Ctrl+R": paste the content of the default register (directly after the caret, so without the newline);
  • Backspace: remove the last newline.

You get:

'some text
that I'd like
to move'
1
  • 1
    Thanks, that's a great help! I guess I'll have to look more into the details of using registers. :)
    – Kjartan
    Commented Oct 18, 2019 at 7:52
6

Another way is to create :h characterwise-register to paste into ''.

place your cursor any place in the paragraph and do:

vipv$hd

move to first '

p

done.

  • vip select paragraph, :h ip is linewise, which means current visual mode is V.
  • v change mode to v, which is character wise, check :h v_v if you have doubt.
  • $h select until last character of last line, h is used to exclude trailing linebreak.
  • d delete into to register.

Update

@Rich mentioned v}ged in the comment, it's 2 characters shorter, it doesn't involve count, although you must place cursor at start of the paragraph, it's still a very intuitive and handy method.

4
  • 1
    I like it. I'd do V2jvg_d instead, but for the most part same idea. Actually, this also works: v3g_d, two characters shorter (for the Vimgolfer in me...)
    – filbranden
    Commented Oct 18, 2019 at 8:46
  • 1
    @filbranden It's good to know g_, i have never used it. v3g_d is shorter, but it involves a count, count is bad.
    – dedowsdi
    Commented Oct 18, 2019 at 8:51
  • 2
    If we're golfing (but also trying to avoid counts), how about v}ged?
    – Rich
    Commented Oct 18, 2019 at 10:27
  • @Rich ge is another useful command I barely used, although it still involves place cursor at the start of the paragraph, I believe it's an useful style.
    – dedowsdi
    Commented Oct 18, 2019 at 10:38

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.