copy/paste of large amount of text to terminal leads to scrambled/missing characters [duplicate]

I work with TextMate and R over the terminal. When pasting a lot of code (CMD+C/CMD+V, e.g. 60 lines) it sometimes happens that few characters in the code are scrambled, which then leads to error messages and wrong code execution.

For example:

col <- ifelse(...

is turning into

col < col < cse(...

or

onlyDF

is turning into

oalyDF

These character-replacements seem very random and it is difficult to predict when it happens.

The problem is also described here for iterm2: https://gitlab.com/gnachman/iterm2/issues/3160

The explanation provided there is that it is a bug in bash that can lead to buffered input to get lost due to paste speed.

Has anyone experienced these problems and found a work-around for the native terminal app in Mac OS? (e.g. by modifying the paste speed, if that is possible in terminal)


When I need to paste a lot of text into a Terminal application window, I use the cat command to transfer the text to a file. For example, I would enter the command below before pasting the text with the +V key combination.

cat >myfile.txt

When finished pasting text, enter the control+D key combination. This will terminate the cat command.

Note: The control+D key combination must be entered at the beginning of a line. If not at the beginning of a line after pasting the text, then press the return key before entering the control+D key combination.


You can use the pbpaste command to get the text in the pasteboard without going through the vagaries of the terminal driver, buffer overruns, etc. To put the cut text into a new file, use:

pbpaste >myfile.txt

To append, use >>, etc. Similarly, you can feed the pasteboard's contents to other commands as input:

pbpaste | somecommand

And if you use the vi editor, you can insert the pasteboard's contents into your file with:

:r !pbpaste

BTW, there's also a corresponding pbcopy command to put text into the pasteboard. Unlike standard file redirections, you can use this to nondestructively modify the contents of the pasteboard "in place":

pbpaste | sed 's/foo/bar/' | pbcopy

...and if you want to see the output as well:

pbpaste | sed 's/foo/bar/' | tee /dev/tty | pbcopy