views:

731

answers:

5

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 two 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
+1  A: 

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
p and P pastes from the VIM buffer, not from the Windows clipboard, so that doesn't work in this case
Rabarberski
Missed that part, edited to add info.
Michael Foukarakis
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
+1  A: 

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
The question was not how to paste, but how to paste at the cursor given the clipboard content contains two lines (separated by a carriage return)
Rabarberski
I believe I understood your question. Are you saying that you tried "+gP and it didn't work? It does for me.
Pierre-Antoine LaFayette
I've updated my response so that it is directly answering your question.
Pierre-Antoine LaFayette
+1  A: 
:set paste

before you paste something into the buffer

:set nopaste

to restore formatting settings

maksymko
nice try, but doesn't work for me. The 2 lines still get pasted ABOVE the current line
Rabarberski
+3  A: 

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)

Peter van der Heijden
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
Ah, your CTRL-R * update is doing it for me!
Rabarberski
+2  A: 

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
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
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