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).