Skip to content
shellhacks.dev
Command Line

Send Keys to a tmux Pane from a Script

Type into a pane from a script with tmux send-keys -t name 'command' Enter. Words like Enter are key names, and -l sends the text as typed instead.

1 min read
Summarize in AI

A script should start a command in a tmux session and push input to it without attaching.

Shell
tmux new -d -x 80 -y 24 -s demo "env PS1='$ ' bash --norc"
tmux send-keys -t demo 'echo hi' Enter
tmux send-keys -t demo -l 'Enter'
sleep 0.3
tmux capture-pane -p -t demo | head -4
tmux kill-server
Text
$ echo hi
hi
$ Enter

Each argument after the target is either text, which is typed as it is, or the name of a key such as Enter, C-c or Up. The name of a key is only recognized as a whole argument, so 'echo hi' Enter types the text and then presses return. With -l everything is literal, and the word Enter is typed as five letters, as the last line of output shows. The key names are listed in the tmux manual.

send-keys is asynchronous. It returns when the keys are queued, not when the command has finished, so a script that reads the pane next needs a short wait or a marker to poll for. Typing into a pane that runs a program other than a shell sends keys to that program. For a one-shot command, tmux new -d 'command' is simpler.

Share

More snippets

Command Line

Avoid Parsing ls Output in Shell Scripts

Do not loop over the output of ls in a script. File names with spaces or a newline split into pieces, while a glob such as ./* hands over each name whole.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.