if-shell
Short form: if
Run the first command if shell-command succeeds, otherwise the optional second command. With -F, no shell runs and the condition is a format that is true unless empty or zero.
How if-shell works
if-shell runs one tmux command when a shell command succeeds and, optionally, another when it fails. Its main use is making one .tmux.conf work on several machines: different settings for macOS and Linux, or bindings that only appear when a tool is installed.
The condition is run with /bin/sh, and success means an exit status of 0. Formats in it are expanded first. With -F no shell runs at all: the condition is a format that counts as true unless it is empty or 0, which is faster and good for checks tmux already knows about.
-b runs the shell command in the background so tmux doesn't wait for it.
Synopsis
tmux if-shell [-bF] [-t target-pane] shell-command command [command]Options
| Flag | Description |
|---|---|
| -b | Run shell-command in the background (don't block) |
| -F | Treat the condition as a format, not a shell command |
| -t target-pane | Target pane for the commands |
Examples
Source a file only if it exists
if-shell 'test -f ~/.tmux.local' 'source ~/.tmux.local'Apply host-specific config without spawning a shell
if-shell -F '#{==:#{host},laptop}' 'source ~/.tmux-laptop.conf'In .tmux.conf: on macOS only, make y in copy mode copy to the system clipboard.
if-shell 'uname | grep -q Darwin' 'bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel pbcopy'In .tmux.conf: only add the lazygit popup on machines that have lazygit.
if-shell 'command -v lazygit >/dev/null' "bind g display-popup -E -w 90% -h 90% lazygit"Default Keybinding
if-shell has no default key. Run it from a shell as tmux if-shell, type it at the command prompt (Ctrl+b then :), or give it a key with bind-key.
Common Mistakes
- Each check without -F starts a shell, and tmux waits for it. Many checks in a config slow down startup; prefer -F where you can.
- Quoting nests quickly: the whole command is one argument and each branch is another. Use single quotes outside and double quotes inside.
- With -b, tmux doesn't wait, so later lines in your config may run before the branch does. Don't use -b when later settings depend on the result.
Pro Tips
- •Use -F with formats to branch without the cost of a shell
- •Great for making one .tmux.conf portable across machines