tmux configuration conditional to OS

42

13

There's some lines of my tmux.conf which I'd like executed only if my OS is Mac. However, I'd like to use my tmux.conf on multiple different OS's. How can I make a command conditional to the OS on which tmux is currently running?

muckabout

Posted 2013-01-21T20:25:28.440

Reputation: 521

1

version detect http://stackoverflow.com/questions/35016458/how-to-write-if-statement-in-tmux-conf-to-set-different-options-for-different-t

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2016-07-06T20:46:13.363

Answers

51

Use the if-shell command:

if-shell "uname | grep -q Darwin" "tmux-cmd1; tmux-cmd2;" "tmux-cmd3; tmux-cmd4"

You may want to put OS-specific commands in separate files, and execute them via the "source-file" command.

if-shell "uname | grep -q Darwin" "source-file .tmux-macosx" "source-file .tmux-linux"

chepner

Posted 2013-01-21T20:25:28.440

Reputation: 5 645

2This should be accepted; it's the proper way to do it. – Chev – 2014-11-14T18:06:15.987

8The if-shell and run-shell tmux commands are currently asynchronous (as of tmux 1.7); they effectively run their shell command in the background, and any tmux commands that they run will only be executed after any commands that come after the if-shell or run-shell command itself (tmux is single-threaded). Effectively, if you use if-shell or run-shell in ~/.tmux.conf, the initial session (and any sessions, windows, or panes created explicitly created through ~/tmux.conf) will lack any tmux configuration arranged through if-shell or run-shell commands. – Chris Johnsen – 2013-01-22T03:01:42.013

1

@ChrisJohnsen if-shell works as expected for me with tmux 1.8. I am using it to set set-titles-string only for SSH: https://github.com/blueyed/dotfiles/commit/cd9e2b0115f579af0954ea89d9b542d4230df2e3

– blueyed – 2014-02-19T13:59:20.077

12

Jimeh https://github.com/jimeh/dotfiles/commit/3838db8 has the answer. Also Chris Johnsen deserves a lot of credit for helping people on the GitHub issue here: https://Github.com/ChrisJohnsen/tmux-MacOSX-pasteboard/issues/8#issuecomment-4134987

Basically, you set up a shell script called safe-reattach-to-user-namespace that checks for the existence of the real reattach... command.

#! /usr/bin/env bash

# If reattach-to-user-namespace is not available, just run the command.
if [ -n "$(command -v reattach-to-user-namespace)" ]; then
  reattach-to-user-namespace $@
else
  exec "$@"
fi

Ivan

Posted 2013-01-21T20:25:28.440

Reputation: 433