I've seem some examples to achieve what I am trying to do using if-shell
, but I'd like to do it only with 'native' tmux commands. By 'native' I mean not spawning another process.
if-shell
may or may not spawn a shell. See the description:
if-shell [-bF] [-t target-pane] shell-command command [command]
(alias: if
)
Execute the first command
if shell-command
returns success or the second command otherwise. Before being executed, shell-command
is expanded using the rules specified in the FORMATS section, including those relevant to target-pane
. With -b
, shell-command is run in the background.
If -F
is given, shell-command
is not executed but considered success if neither empty nor zero (after formats are expanded).
(source)
Any command denoted command
is a tmux command, it doesn't need a shell. shell-command
needs a shell unless -F
is given. Not every test can be performed with -F
but in your case it's easy. This will not spawn another process:
tmux if-shell -F '#{window_zoomed_flag}' '' 'resize-pane -Z'
To confirm that if-shell -F
spawns no additional processes, do the following:
- Work inside tmux, so tmux server exists. You will need two panes; create them beforehand.
Store the PID of the tmux server:
pid="$(pgrep 'tmux: server')"
Verify the variable is not empty, just in case:
echo "$pid"
Use strace
to tell if the server creates new processes (see this answer):
strace -e fork,vfork,clone,execve -fb execve -p "$pid"
Select the other pane and invoke:
tmux if-shell -F '#{window_zoomed_flag}' '' 'resize-pane -Z'
# now similar command to unzoom
tmux if-shell -F '#{window_zoomed_flag}' 'resize-pane -Z'
Observe what strace
printed. There should be no output.* This means no additional process was spawned.
For comparison, these are equivalent commands without -F
, they do spawn shells:
tmux if-shell '[ #{window_zoomed_flag} -eq 1 ]' '' 'resize-pane -Z'
# now similar command to unzoom
tmux if-shell '[ #{window_zoomed_flag} -eq 1 ]' 'resize-pane -Z'
Each command will make execve("/bin/sh", …
appear in the output from strace
.
* Unless you use a hook triggered by a pane being zoomed or resized, and the hook spawns another process. Temporarily remove the hook so it doesn't interfere with the test.