Rabarberski
Rabarberski

Reputation: 24922

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:

Upvotes: 9

Views: 3595

Answers (5)

Brian Carper
Brian Carper

Reputation: 72926

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

Upvotes: 2

heijp06
heijp06

Reputation: 11788

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)

Upvotes: 12

Pierre-Antoine LaFayette
Pierre-Antoine LaFayette

Reputation: 24402

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

"+gP
Don't forget the double quote.

Upvotes: 1

Maxim Sloyko
Maxim Sloyko

Reputation: 15866

:set paste

before you paste something into the buffer

:set nopaste

to restore formatting settings

Upvotes: 1

Michael F
Michael F

Reputation: 40830

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.

Upvotes: 1

Related Questions