5

I often use a REPL-style coding method when writing shell scripts (or other relevant languages), and recently noticed the following issue. I run tmux so I can have my script open in vim in a pane side-by-side with a terminal prompt.

Tmux

If I try to paste multiple lines of commands at once using CMD-v on a Mac, i.e.

a=hello
b=World
echo $a $b

tmux does not process the newlines properly, but instead gives the following output:

[user@host: ~]$ a=hello
b='World'
echo $a $b
[user@host: ~]$ b='World'echo $a $b

If I clear the prompt, and run echo $a, I get hello echo'ed to the screen, but echo $b produces an empty line, and obviously the echo $a $b line does not get run.

I get the same output using a REPL like gnuplot, or when using rlwrap.

Alternate tmux attempt

The same issue occurs when using vim-slime, or using the relevant vim-slime system calls manually:

[user@host: ~]$ tmux set-buffer 'a=hello
> b=World
> echo $a $b
> '
[user@host: ~]$ tmux paste-buffer -p
a=hello
b=World
echo $a $b
[user@host: ~]$ a=hellob=Worldecho $a $b

I have tried tmux paste-buffer with, and without the -p flag for bracketed paste mode.

Plain bash shell, or GNU screen

If I perform the same CMD-v paste action in a normal bash shell (not in tmux), I get:

[user@host: ~]$ a=hello
[user@host: ~]$ b=World
[user@host: ~]$ echo $a $b
hello World
[user@host: ~]$

as expected. I get the same output when pasting in GNU screen (v4.04.00).

Question

Why does tmux not process the pasted commands line-by-line, as bash/gnu screen do? How do we fix this problem?

Already asked?

The same issue appears to have been asked at this stackoverflow question, and this other stackoverflow question, but not yet answered satisfactorily.

This answer offers a solution of a sleep line between each command, which does the trick, but it's a bit of a hack to assume how long each command will take to process before sending the next line of text. There must be a better way.

Versions

I am running Mac OS X El Capitan (v10.11.6), iTerm2 (v3.0.10), tmux (v2.2), GNU bash (v4.4.0).

The same results can be reproduced using Terminal.app (v2.6).

Community
  • 1
  • 1
Bernie Roesler
  • 329
  • 3
  • 11
  • 1
    Can you reproduce this with completely empty .tmux.conf and .bashrc files? Are you using some sort of clipboard manager that could be coming into play? This is curious, as I'm not having this issue, and I'm running the same versions of OSX, iTerm2, and tmux. – Alex Torok Sep 26 '16 at 14:48
  • Ah, I am using `reattach-to-user-namespace` as a wrapper to get access to the mac os clipboard ([this github repo](https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard/blob/master/README.md)), which seems to be causing the problem. Copy/paste seems to be a constant headache with vim/tmux/mac os so I'd appreciate any suggestions for a solution. – Bernie Roesler Oct 05 '16 at 20:18

1 Answers1

2

I solved the problem. I had been using reattach-to-user-namespace to interact with the OS X clipboard; however, according to the reattach-to-user-namespace github page:

Note: Under Yosemite (and later) pasteboard access seems to work fine without the program from this repository.

I removed the set-option -g default-command "reattach-to-user-namespace -l bash" line from my .tmux.conf file. I also changed my tmux mapping to

bind -t vi-copy y copy-pipe "pbcopy"

and it copies text to the OS X clipboard from vi-copy mode as expected. Pasting text using the OS X default Cmd-v produces the expected behavior (like in screen or plain bash shell as described in the question). Thanks to @Alex Torok for prompting my config file debugging.

Bernie Roesler
  • 329
  • 3
  • 11