How to use tmux send-keys command?

Quick Answer

The tmux send-keys command allows you to send keystrokes to a tmux pane or window. This is useful for automating tasks, running commands in multiple panes simultaneously, or controlling remote sessions. Use it with tmux send-keys -t target "command" Enter.

$ tmux send-keys -t session:window.pane "echo hello" Enter

Detailed Explanation

The send-keys command is a powerful tmux feature that lets you programmatically send keystrokes to any pane or window, even if it's not currently active or visible.

Basic syntax:

tmux send-keys -t TARGET "TEXT" [KEY ...]

Important parameters:

  • -t target - Specifies which session, window, or pane to send keys to
  • -l - Send literally (don't interpret special key names)
  • -R - Reset terminal state before sending

Target specification formats:

  • session - Send to active pane of specified session
  • session:window - Send to active pane of specified window
  • session:window.pane - Send to specific pane
  • :window.pane - Send to pane in current session
  • .pane - Send to pane in current window

Common usage examples:

  • tmux send-keys -t mysession "ls -la" Enter - Run ls command
  • tmux send-keys -t 0:1.2 "cd /home/user" Enter - Change directory in session 0, window 1, pane 2
  • tmux send-keys -t dev Up Enter - Send Up arrow key and Enter
  • tmux send-keys -t all "echo synced command" Enter - Send to all panes (with proper setup)

Special key names:

You can send special keys by name:

  • Enter
  • Escape or Esc
  • Tab
  • Space
  • BSpace or BackSpace
  • Up, Down, Left, Right
  • DC (Delete)
  • Home, End
  • F1 through F12

Pro Tip: Synchronize Panes

To send the same command to all panes in a window, you can use the synchronize-panes feature:

# Enable synchronize-panes
tmux set-window-option synchronize-panes on

# Type commands (will go to all panes)
# ...

# Disable when done
tmux set-window-option synchronize-panes off