Python – Why does xclip-copied text pasted into Gmail on Firefox turn line breaks into spaces

firefoxpython3xclip

I have a text editor I made that uses xclip to copy text. I notice that when I paste the xclip-copied text (pasting the regular way, with ctrl+v) into a Gmail email on Firefox 54.0 (64-bit) that I'm composing, it replaces all of the line breaks with spaces (and if there are multiple line breaks in a row, it turns all of them collectively into a single space). When I paste it elsewhere, I don't have this issue. However, when I copy from somewhere like Leafpad and paste it into a Gmail email in Firefox, it retains the line breaks. The same problem doesn't happen with Gmail in the Chromium web browser.

Why does this happen, and is there a way to fix it so it doesn't strip out the line breaks?

I'm using xclip 0.12 on Xubuntu 16.10, 64-bit. Here's the command I use to copy the text:

xclip -selection clipboard [seeBelow]

I use this line of Python code to insert the text where I have [seeBelow] above:

subprocess.Popen(command, stdin=subprocess.PIPE).communicate(text.encode());

I am copying the text from a Python3 Tkinter multi-line Text widget (well, a widget that inherits Text, and should be practically the same for our purposes).

The problem just seems to happen in Gmail within Firefox. That is a place I frequently like to paste, however, as I often write my emails in my editor.

Best Answer

This drove me crazy for a while, too. I could copy and paste from various sources, but somehow anything from xclip or nvim-qt (a NeoVim GUI) resulted in collapsing line breaks into spaces. Incidentally, the same problem appears on Slack and Rocket Chat, but apparently not on Stack Exchange. Meanwhile, in the same textareas, the in-browser vim clone wasavi has the opposite problem: doubling each line break.

My workaround is to use a simple replacement for xclip. The code, tested in Python 2.7, reads as follows:

#!/usr/bin/env python

import pyperclip   # pip install --user pyperclip
import sys

filename = sys.argv[1]

with open (filename) as f:
    pyperclip.copy (f.read ())

Similarly, you can pull text from the clipboard with s = pyperclip.paste().

Related Question