0

When I want to paste some text after selecting vertically with <C-q>

enter image description here

It is known that Vim/gVim will replace the character under the cursor. For example, below is shown the result after pasting hello using "+P after <C-q>.

enter image description here

It is shown how the first column is being 'replaced'.

Is there some option to avoid this behavior instead of manually inserting a space to the selection and then re-select and paste?

PS: I know that in this particular example I could insert the text using s command, but my question is for a general use case.

3 Answers 3

2

That behavior is the normal behavior of p and P in all visual modes. It is the only desirable behavior, actually, and it happens to match how more familiar editors work:

  • you select the text you want to "paste over",
  • you "paste over" the selected text.

If that's not what you want to happen, then use a more appropriate command. In this case (assuming you want to prepend the content of the register to the selected text):

I<C-r>" <Esc>

hw

1
  • That's great using <C-r>+ in my case ! Pasting using "+P after <C-q>I brakes the mode and the pasted text is not propagated to the rest of lines. Using <C-r>+ works like a sharm. I finally setup inoremap <silent> <nowait> <C-Space> <C-r>+ (Space is near the v) to easily do the pasting without breaking the mode. Thanks!
    – xyx
    Jul 5 at 21:20
1

Another option: get a range for the insertion (in this case, I'd probably use vip:), then do

:[range]substitute/^/\=@+

(abbreviate the :substitute to :s as desired).

As pointed out, if you need to manipulate the result of the paste further you need string concatenation (or other VimScript expressions); for simple things, I would probably use the I<C-r>= method. An abbreviated example with a space after the paste:

:s/^/\=@+..' '
5
  • missing a space? Jul 6 at 13:56
  • @ChristianBrabandt do you mean you want \=@+ .. ' '? You could; the OP isn't clear about the desired results
    – D. Ben Knoble
    Jul 6 at 14:06
  • oh I thought, that was his question. but yes, something along those lines Jul 6 at 15:41
  • I explicitly written that in this particular case I know I can insert the yanked text with the substitute command, and I was interested in a general case, so when the columns are not at the first one ^, but, for example, are indented... Pasting without overwriting the character under cursor (which is what p and/or P do) is useful when coding.
    – xyx
    Jul 6 at 20:20
  • Maybe other readers will appreciate this answer. I do. BTW, to allow for indented lines, use e.g. [range]:s/\s*\zs/\=@+. Just the first thing that came to my mind.
    – Friedrich
    Jul 7 at 6:16
0

The solution of @romainl works fine as long as the buffer contains a normal selection. If it is containing a block selection I would do: 1v5>PgvloWhd

Where:

  • 1v select a block of the shape of your clipboard
  • 5> make enough room to insert the block
  • P paste the block
  • gvloWh select the not necessary white spaces
  • d delete them

This assume that the first line of the block is made of one WORD

Your Answer

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

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