1

According to tmux man page, it is possible to use %if to conditionally run its commands. So I am trying to use it to zoom the current pane if it is not already zoomed (otherwise it would un-zoom it).

I tried some variations of

%if '#{window_zoomed_flag}' resize-pane -Z %endif

without success. Any ideas?

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.

2
  • Thanks, fixed the title. About your commend, %if works for me not only in configuration files: it works also pressing ctrl b and then ':'. But not with that variable, and I don't know why. Commented Nov 27, 2019 at 19:16
  • If -F is given, shell-command is not executed but considered success if neither empty nor zero (after formats are expanded). It seems you are right. Thanks! Commented Nov 27, 2019 at 19:33

1 Answer 1

1

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:

  1. Work inside tmux, so tmux server exists. You will need two panes; create them beforehand.
  2. Store the PID of the tmux server:

    pid="$(pgrep 'tmux: server')"
    
  3. Verify the variable is not empty, just in case:

    echo "$pid"
    
  4. Use strace to tell if the server creates new processes (see this answer):

    strace -e fork,vfork,clone,execve -fb execve -p "$pid"
    
  5. 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'
    
  6. Observe what strace printed. There should be no output.* This means no additional process was spawned.

  7. 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .