通过遍历panes来熟悉如何精准的选择tmux的pane,这是一些自动化脚本的基础。
脚本
#!/bin/bash
# 获取所有tmux会话的列表
sessions=$(tmux list-sessions -F '#{session_name}')
# 遍历每个会话
for session in $sessions; do
# 获取会话中的所有窗口列表
windows=$(tmux list-windows -t "$session" -F '#{window_id}')
# 遍历每个窗口
for window in $windows; do
# 获取窗口中的所有 pane 列表
panes=$(tmux list-panes -t "$session:$window" -F '#{pane_id}')
# 遍历每个 pane
for pane in $panes; do
# 发送Ctrl-C信号给窗口中的每个pane中的程序(前台进程组)
# tmux send-keys -t "$session:$window.$pane" C-c
# 发送回车信号给窗口中的每个pane中的程序(前台进程组)
tmux send-keys -t "$session:$window.$pane" C-m
done
done
done
-F 说明
-F
选项用于指定输出的格式,#{}
用于指定输出的内容,list-sessions
、list-windows
、list-panes
命令都支持 -F
选项。
节选自 man tmux
:
Sessions, window and panes are each numbered with a unique ID; session IDs are prefixed with a ‘$’, windows with a ‘@’, and panes with a ‘%’. These are unique and are unchanged for the life of the session, window or pane in the tmux server. The pane ID is passed to the child process of the pane in the TMUX_PANE environment variable. IDs may be displayed using the ‘session_id’, ‘window_id’, or ‘pane_id’ formats (see the FORMATS section) and the display-message, list-sessions, list-windows or list-panes commands.
...
FORMATS
Certain commands accept the -F flag with a format argument. This is a string which controls the output format of the command. Format variables are enclosed in ‘#{’ and ‘}’, for example
‘#{session_name}’. The possible variables are listed in the table below, or the name of a tmux option may be used for an option's value. Some variables have a shorter alias such as ‘#S’;
‘##’ is replaced by a single ‘#’, ‘#,’ by a ‘,’ and ‘#}’ by a ‘}’.
Variable name Alias Replaced with
alternate_on 1 if pane is in alternate screen
alternate_saved_x Saved cursor X in alternate screen
alternate_saved_y Saved cursor Y in alternate screen
buffer_created Time buffer created
buffer_name Name of buffer
buffer_sample Sample of start of buffer
buffer_size Size of the specified buffer in bytes
client_activity Time client last had activity
client_control_mode 1 if client is in control mode
client_created Time client created
client_discarded Bytes discarded when client behind
client_height Height of client
client_key_table Current key table
client_last_session Name of the client's last session
client_name Name of client
client_pid PID of client process
client_prefix 1 if prefix key has been pressed
client_readonly 1 if client is readonly
client_session Name of the client's session
client_termname Terminal name of client
client_termtype Terminal type of client
client_tty Pseudo terminal of client
client_utf8 1 if client supports utf8
client_width Width of client
client_written Bytes written to client
command Name of command in use, if any
command_list_alias Command alias if listing commands
command_list_name Command name if listing commands
command_list_usage Command usage if listing commands
cursor_character Character at cursor in pane
cursor_flag Pane cursor flag
cursor_x Cursor X position in pane
cursor_y Cursor Y position in pane
history_bytes Number of bytes in window history
history_limit Maximum window history lines
history_size Size of history in lines
hook Name of running hook, if any
hook_pane ID of pane where hook was run, if any
hook_session ID of session where hook was run, if any
hook_session_name Name of session where hook was run, if any
hook_window ID of window where hook was run, if any
hook_window_name Name of window where hook was run, if any
host #H Hostname of local host
host_short #h Hostname of local host (no domain name)
insert_flag Pane insert flag
keypad_cursor_flag Pane keypad cursor flag
keypad_flag Pane keypad flag
line Line number in the list
mouse_all_flag Pane mouse all flag
mouse_any_flag Pane mouse any flag
mouse_button_flag Pane mouse button flag
mouse_line Line under mouse, if any
mouse_sgr_flag Pane mouse SGR flag
mouse_standard_flag Pane mouse standard flag
mouse_utf8_flag Pane mouse UTF-8 flag
mouse_word Word under mouse, if any
mouse_x Mouse X position, if any
mouse_y Mouse Y position, if any
origin_flag Pane origin flag
pane_active 1 if active pane
pane_at_bottom 1 if pane is at the bottom of window
pane_at_left 1 if pane is at the left of window
pane_at_right 1 if pane is at the right of window
pane_at_top 1 if pane is at the top of window
pane_bottom Bottom of pane
pane_current_command Current command if available
pane_current_path Current path if available
pane_dead 1 if pane is dead
pane_dead_status Exit status of process in dead pane
pane_format 1 if format is for a pane (not assuming the current)
pane_height Height of pane
pane_id #D Unique pane ID
pane_in_mode 1 if pane is in a mode
pane_index #P Index of pane
pane_input_off 1 if input to pane is disabled
pane_left Left of pane
pane_marked 1 if this is the marked pane
pane_marked_set 1 if a marked pane is set
pane_mode Name of pane mode, if any
pane_pid PID of first process in pane
pane_pipe 1 if pane is being piped
pane_right Right of pane
pane_search_string Last search string in copy mode
pane_start_command Command pane started with
pane_synchronized 1 if pane is synchronized
pane_tabs Pane tab positions
pane_title #T Title of pane
pane_top Top of pane
pane_tty Pseudo terminal of pane
pane_width Width of pane
pid Server PID
rectangle_toggle 1 if rectangle selection is activated
scroll_position Scroll position in copy mode
scroll_region_lower Bottom of scroll region in pane
scroll_region_upper Top of scroll region in pane
selection_present 1 if selection started in copy mode
session_activity Time of session last activity
session_alerts List of window indexes with alerts
session_attached Number of clients session is attached to
session_created Time session created
session_format 1 if format is for a session (not assuming the current)
session_group Name of session group
session_group_list List of sessions in group
session_group_size Size of session group
session_grouped 1 if session in a group
session_id Unique session ID
session_last_attached Time session last attached
session_many_attached 1 if multiple clients attached
session_name #S Name of session
session_stack Window indexes in most recent order
session_windows Number of windows in session
socket_path Server socket path
start_time Server start time
version Server version
window_active 1 if window active
window_activity Time of window last activity
window_activity_flag 1 if window has activity
window_bell_flag 1 if window has bell
window_bigger 1 if window is larger than client
window_end_flag 1 if window has the highest index
window_flags #F Window flags
window_format 1 if format is for a window (not assuming the current)
window_height Height of window
window_id Unique window ID
window_index #I Index of window
window_last_flag 1 if window is the last used
window_layout Window layout description, ignoring zoomed window panes
window_linked 1 if window is linked across sessions
window_name #W Name of window
window_offset_x X offset into window if larger than client
window_offset_y Y offset into window if larger than client
window_panes Number of panes in window
window_silence_flag 1 if window has silence alert
window_stack_index Index in session most recent stack
window_start_flag 1 if window has the lowest index
window_visible_layout Window layout description, respecting zoomed window panes
window_width Width of window
window_zoomed_flag 1 if window is zoomed
wrap_flag Pane wrap flag
总结:
- sessions, windows, panes 都有唯一的 ID,分别以
$
、@
、%
开头。 - 可以通过
-F
选项指定输出的格式,#{}
用于指定输出的内容。 - list-sessions、list-windows、list-panes 命令都支持
-F
选项。 - 遍历时用到三种格式:
#{session_name}
、#{window_id}
、#{pane_id}
。