9

When I copy a two-line text from (e.g.) a PDF opened in Acrobat Reader into gvim using CTRL-V, the text is inserted above the line in gvim where I was positioned, instead of at the position where my cursor is. (scenario: I want to copy a document title that is spread over two lines and paste it in between a html tag in gvim).

If I do the same with a single-line of text, the text is correctly pasted at the cursor's position.

What should I do to make it also work with two-lines of text (e.g. something like 'paste without formatting')?

Important: the string to be pasted consists of two lines separated by a carriage return (or something similar)!

Solution

There are actually two valid solutions:

  • using CTRL-R * to paste at the cursors position (and keeping the clipboard content multi-lined)
  • do a remapping of the paste command to replace all carriage returns in the clipboard string
Rabarberski
  • 23,854
  • 21
  • 74
  • 96

5 Answers5

12

First of all it seems CTRL-V is mapped to something because normally pasting in VIM is not done using CTRL-V (CTRL-V is visual block).

Second the behaviour you are seeing is standard vi[m] behaviour, if you yank one or more entire lines, pasting will always result in one or more lines above or below the current line.

I do not know of any builtin way to achieve what you are trying to do. But you could always remap CTRL-V to something that does do what you want, i.e.

:map <C-V> i<CR><Esc>"*P

After that pasting multiple lines will be between the characters your cursor was at, but this remapping probably is not what you want in other cases.

EDIT

OK, I found another way.

When in insert mode, type

CTRL-R *

this will paste the contents of the clipboard buffer at the location the cursor is at. (You can also use this to paste the contents of other buffers)

heijp06
  • 11,558
  • 1
  • 40
  • 60
  • Ah, finally somebody who understood my problem :-) I see what you are trying to do, and it is indeed only a half-working workaround. Strange that vim can't handle this properly. Isn't there a way to remove carriage returns in the buffer (clipboard) before pasting it into VIM? (BTW: the non-standard ctrl-v shortcut isn't the problem here) – Rabarberski Sep 29 '09 at 13:26
2

If you want to strip newlines from a register before pasting from it, you can use the expression register "=:

:map <C-v> "=substitute(@*, "\n", " ", "g")<CR>p
Brian Carper
  • 71,150
  • 28
  • 166
  • 168
  • Very nice tip. It took me some experimenting to realize this only works when NOT in insert mode. But I can use :imap to map it there as well. Thanks! – Rabarberski Sep 30 '09 at 07:38
  • Small update: I've altered you're solution by using P instead of p at the end of the mapping command. This pastes it after the cursor, which feels more natural to me. – Rabarberski Sep 30 '09 at 07:42
1

Use the shortcuts p and P (paste after and before the cursor, respectively). Add "* to specify contents of the system register (Windows clipboard, in your case), or "+ (for UNIX).

So, you can use:

  • "*p paste before cursor
  • "*P paste after cursor
  • "*y copy (visual area, or takes a postfix telling Vim "what" to copy)
  • "*d cut (visual area, or with a postfix)

and so forth.

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
  • p and P pastes from the VIM buffer, not from the Windows clipboard, so that doesn't work in this case – Rabarberski Sep 29 '09 at 08:13
  • Hmm, thanks for the tip, but that still doesn't work. It gets pasted ABOVE the line where I am positioned, instead of at the cursors position – Rabarberski Sep 29 '09 at 08:27
1
:set paste

before you paste something into the buffer

:set nopaste

to restore formatting settings

Maxim Sloyko
  • 15,176
  • 9
  • 43
  • 49
0

To paste into gVim from the windows clipboard at the position of the cursor use

"+gP
Don't forget the double quote.
Pierre-Antoine LaFayette
  • 24,222
  • 8
  • 54
  • 58