os/tmux/ TmuxIntermediate
See https://github.com/tmux/tmux/wiki/Advanced-Use
Socket
Tmux by default creates a socket in /tmp/tmux-$UID/default
.
We can change this
tmux -Lhello -- creates/uses server and socket in /tmp/tmux-$UID/hello
tmux -Shello -- creates/uses server and socket in file 'hello'
to see socket path within a session
:display -p '#{socket_path}'
If a socket is accidentally deleted, it can be recreated via
pkill -USR1 tmux
Session
:kill-session -C -- clear alert flags
Working directory
By default the working directory is the directory we were in when launching tmux. This can be changed via
:attach -c/tmp
Or when creating a new session, window or pane
:new -c/tmp -- new session with working directory /tmp
:neww -c/tmp -- new window with working directory /tmp
:splitw -c/etc -- new pane (split one above other) with cwd=/etc
:splitw -h -c/usr -- new pane (split one beside other) with cwd=/usr
Respawning pane
By default, when the command running in a pane exits, the pane closes. We can change this
:set-option remain-on-exit
and then
:respawn-pane -- start new process in a pane
:respawn-pane cmd -- run cmd in (dead) pane
:respawn-pane -k cmd -- kill current process and run cmd in pane
Window sizes
:show -g default-size
:set-option window-size largest/smallest/latest/manual
Then there are keys when the window is larger than our terminal: {{Ctrl-B}} followed by
S-Up
S-Down
S-Left
S-Right
Delete -- return to tracking the cursor position
and commands
:resizew -x200 -y100 -- resize to 200x100
:resizew -A -- work out size from attached clients
:resizew -L20 -- adjust size left
:resizew -R20, -D20, -U20 -- same but for right, down, up
Piping pane changes
:pipe-pane 'cat > /tmp/log1' -- pipes changes to terminal to cat
:pipe-pane -- stop piping
:pipe-pane -I 'cat /tmp/log1'
:pipe-pane -o 'cat > /tmp/log1' -- toggles piping
Pane commands
:selectp -Tmytitle -- set title for pane
The pane title is in quotes to the lower right, the window name is next to its number, and the session name is in square brackets to the lower left.
Terminal window stuff
With e.g. xterm and friends, we can set the title using
set -g set-titles on
and change what the title is set to via
set -g set-titles-string '#{pane_title}'
where '#{pane_title}'
is a format string for the title.
By default this is 'session-name:window-number:window-name - pane-name'
.
Format strings
For things like setting the title, the format string looks like e.g.
'#{pane_title}'
and there are a few variables we can use in here. These are
#{pane_title} -- pane title
#{session_name}, #{window_name} -- names
#{pane_id}, #{window_id}, #{session_id} -- ids
For example to list ids in current window
$ tmux lsp -F '#{session_id} #{window_id} #{pane_id}'
$0 @8 %8
Addressing panes
Note that variable TMUX_PANE
is set to the appropriate pane_id
when spawning a pane, and is unique for that pane (it is never reused by a given tmux server).
For example
tmux send -t%22 "echo hello world" Enter -- types echo hello world and presses Enter
tmux send -t%21 -l "echo hello" Enter -- Enter is sent as text
note that this can't send e.g "\n" – that literally sends a backslash followed by n.
tmux capturep -pt%21 -- dump contents of pane %21 to stdout
... -e includes escape sequences for colours and attributes
-C escapes nonprintable chatacters as octal sequences
-N preserves trailing spaces at the end of lines
-J preserves trailing spaces and joins wrapped lines
More panes
Empty pane
$ tmux splitw ''
$ echo 'hello world' | tmux splitw -I -- takes content of new pane from stdin
Windows
A window can belong to more than one session.
Escape sequences
$ printf '\033]2;newtitle\007' -- sets terminal title
Options
Options, set with :setw