Live data from Hacker News

Scripting tmux

arp242.net

11–20 of 20 posts

Re: Scripting tmux

#11
post #7

This comment was posted using tmux scripting, as follows: set -x send(){ tmux send "$@";} send 'links -no-connect https://news.ycombinator.com/login?goto=news' ; send c-m; sleep 2; send 3xblah c-m $HNPASSWORD c-m c-m c-m; sleep 2; send Escape; sleep 1; send V S; sleep 1; send 'scripting tmux' c-m Down Down Down Down Down Down c-m; send Escape; sleep 1; send V S; sleep 1; send 'add comment' c-m Up; send 'This comment…

Nice, I had no idea you could script tmux like that! Reminds me of "expect", which I've used to automate interactive SSH sessions with network devices.

Re: Scripting tmux

#12
post #11
post #7

This comment was posted using tmux scripting, as follows: set -x send(){ tmux send "$@";} send 'links -no-connect https://news.ycombinator.com/login?goto=news' ; send c-m; sleep 2; send 3xblah c-m $HNPASSWORD c-m c-m c-m; sleep 2; send Escape; sleep 1; send V S; sleep 1; send 'scripting tmux' c-m Down Down Down Down Down Down c-m; send Escape; sleep 1; send V S; sleep 1; send 'add comment' c-m Up; send 'This comment…

Nice, I had no idea you could script tmux like that! Reminds me of "expect", which I've used to automate interactive SSH sessions with network devices.

Replacing the sleeps with calls to "tmux capture-pane" to parse the responses would be closer to expect.

Re: Scripting tmux

#13
post #12
post #11

Earlier quoted context omitted.

Nice, I had no idea you could script tmux like that! Reminds me of "expect", which I've used to automate interactive SSH sessions with network devices.

Replacing the sleeps with calls to "tmux capture-pane" to parse the responses would be closer to expect.

Brainless method I use to check the panel:

  case $1 in
  ""|-b|-E|-S|-t)
  tmux capturep $@ --;
  tmux showb $@ --;
  exec tmux dele --;
  esac
Also there is a "-N repeat count" option for send-keys in newer versions of tmux.

Re: Scripting tmux

#14
post #7

This comment was posted using tmux scripting, as follows: set -x send(){ tmux send "$@";} send 'links -no-connect https://news.ycombinator.com/login?goto=news' ; send c-m; sleep 2; send 3xblah c-m $HNPASSWORD c-m c-m c-m; sleep 2; send Escape; sleep 1; send V S; sleep 1; send 'scripting tmux' c-m Down Down Down Down Down Down c-m; send Escape; sleep 1; send V S; sleep 1; send 'add comment' c-m Up; send 'This comment…

Is this an accidental quine? That's so cool.

[Quine]: https://en.wikipedia.org/wiki/Quine_%28computing%29

Re: Scripting tmux

#15
post #9

Just this night I was thinking if whether tmux scripting could be abused to get it to work like zmodem. Sending files from the system with the tmux session to whatever machine one had sshd into (and ideally the other direction as well). Anyone know of this or a better approach?

Tmux has pipe-pane, and it has -I and -O to go both directions.

It does require some gyrations to get around the fact that it logs everything (the command itself, terminal control, etc), and not just what's output by the command. Here's a quick and dirty example:

a) Start a tmux session to a remote host

b) On the local host, run this (assuming session 0):

$ tmux pipe-pane -o -t0 "tail -n +2 | col -b | base64 -d > ~/output"

$ tmux send-keys -t0 "base64 /some/file && read" Enter

$ tmux pipe-pane -t0

$ tmux send-keys -t0 Enter

You should now have ~/output on the local host, which is identical to /some/file on the remote.

Re: Scripting tmux

#16
post #2

Despite using tmux for years, even having written scripts for nested sessions that I use each day, I hadn't noticed the CLI was missing long options. Kind of crazy.

This is a common property of software with BSD-ish roots. For example, neither does OpenSSH's ssh.

Re: Scripting tmux

#17
post #15
post #9

Just this night I was thinking if whether tmux scripting could be abused to get it to work like zmodem. Sending files from the system with the tmux session to whatever machine one had sshd into (and ideally the other direction as well). Anyone know of this or a better approach?

Tmux has pipe-pane, and it has -I and -O to go both directions. It does require some gyrations to get around the fact that it logs everything (the command itself, terminal control, etc), and not just what's output by the command. Here's a quick and dirty example: a) Start a tmux session to a remote host b) On the local host, run this (assuming session 0): $ tmux pipe-pane -o -t0 "tail -n +2 | col -b | base64 -d > ~/o…

Thank you, that is great! Will certainly play around with that :)

Re: Scripting tmux

#18
post #8
post #6

Have been looking for a while, but does anyone know if there is there anything like which-key for tmux? which-key on vim has been great in making sure, I use new features, I add to my config file.

Prefix + ? will show you the current key map; it is searchable like normal text. Not utterly comfortable, but helps most of the time.

That's nice to know, thanks.

Re: Scripting tmux

#19
post #13
post #12

Earlier quoted context omitted.

Replacing the sleeps with calls to "tmux capture-pane" to parse the responses would be closer to expect.

Brainless method I use to check the panel: case $1 in ""|-b|-E|-S|-t) tmux capturep $@ --; tmux showb $@ --; exec tmux dele --; esac Also there is a "-N repeat count" option for send-keys in newer versions of tmux.

capture-pane has -p so you can pipe its output rather than letting it create a buffer you have to delete.

Re: Scripting tmux

#20
post #13

Earlier quoted context omitted.

Brainless method I use to check the panel: case $1 in ""|-b|-E|-S|-t) tmux capturep $@ --; tmux showb $@ --; exec tmux dele --; esac Also there is a "-N repeat count" option for send-keys in newer versions of tmux.

capture-pane has -p so you can pipe its output rather than letting it create a buffer you have to delete.

s/has/now &/

I started using tmux in 2009. There was no -p option when capturep command was first added. Old scripts that keep working tend not to get updated. I have updated it now. Thank you.

Post reply on HN