user779159
user779159

Reputation: 9602

Paste in vim stripping newlines from the pasted text

I am copying large amounts of text from PDF documents into vim, but when you do a copy-paste from PDFs as opposed to say web browsers, everytime the PDF line breaks within a paragraph the copied text includes a newline. So when I paste into vim using 'p', I then have to press 'J' many times to collapse the newlines.

I'd much rather have a command that I can map to another letter on the keyboard that takes the contents of the copied text and pastes it removing the newlines. Any idea how I can do this?

Upvotes: 3

Views: 1210

Answers (2)

mihai
mihai

Reputation: 38533

This should do it:

map <leader>xx :let @* = substitute(@*, "\n", "", "g")<CR>"*p

Change the <leader>xx with the mapping of your choice.
What this command does, it substitues the end of line charater (\n) with nothing (""), inside the clipboard register (which is the star register). Then it pastes the text from the * register.

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172510

You can use my UnconditionalPaste plugin for that. It provides gcp / gcP mappings that force the paste to be characterwise, i.e. all newlines and indent are flattened to spaces. It also has other, similar mappings to force linewise mode, or paste with a custom separator, etc.

Upvotes: 0

Related Questions