5

I am new to tmux, and find this really strange behaviour when I tried it.

I write a really simple script

tmux new-session -s "test" -d
tmux send-keys -t test hello Enter
tmux attach -t "test" 

when I run it, the shell shows the following

hello
eric:bin$ hello

I only expect the "hello" command shows once inside the prompt, but the "hello" command will show twice: one outside the prompt, one inside the prompt

Does anyone know the reason?

webberpuma
  • 691
  • 1
  • 6
  • 21

2 Answers2

5

I don't think this is a tmux problem. What happens is that tmux is sending the keys to the window before the shell finishes loading.

You can see the phenomenon by doing the following:

$ sleep 3
hello
$ hello
bash: hello: command not found

While the sleep 3 is running you can type "hello" + Enter and only when that program finishes running, bash interprets the input.

You can work around this by making your shell load faster.

tonchis
  • 598
  • 3
  • 10
  • 1
    how to make my shell load faster? furthermore, I don't think my shell is that slow that causes the problem. – webberpuma May 26 '15 at 02:44
  • I am having a similar problem, and looked at the tmux source code to see if there's an easy workaround or fix for this issue. There is not. I got down as far as a call to libevent, at which point I gave up. It would require a lot of rework to edge around the issue of the latency in the shell loading. Something would have to sit in front of libevent and wait for the shell to be ready, then forward all those events at that point. It is definitely doable (and maybe there's a simpler solution?) but that rabbit hole gets a bit deep. – Lord Alveric Oct 21 '15 at 09:54
  • Maybe there's something that can be done during the shell loading side that can grab any incoming "events" and then feed them forward into itself? Hmmmm.... – Lord Alveric Oct 21 '15 at 09:57
1

You can prevent the text showing up twice by inserting sleep 1 between the new-session and send-keys lines. This way the script waits a second for the shell to finish starting up.

This of course makes the whole script take longer. It was a solution for me though because I found it annoying to see the text twice at startup.

rfindeis
  • 461
  • 3
  • 15