Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste multi-line string into GVIM at cursor position

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
like image 383
Rabarberski Avatar asked Sep 29 '09 07:09

Rabarberski


3 Answers

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)

like image 194
heijp06 Avatar answered Nov 02 '22 06:11

heijp06


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
like image 43
Brian Carper Avatar answered Nov 02 '22 06:11

Brian Carper


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.

like image 39
Michael Foukarakis Avatar answered Nov 02 '22 04:11

Michael Foukarakis