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 operating systems. How can I make a command conditional to the OS on which tmux is currently running?
- 2version detect stackoverflow.com/questions/35016458/…Ciro Santilli OurBigBook.com– Ciro Santilli OurBigBook.com2016-07-06 20:46:13 +00:00Commented Jul 6, 2016 at 20:46
3 Answers
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" - 9The
if-shellandrun-shelltmux 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 theif-shellorrun-shellcommand itself (tmux is single-threaded). Effectively, if you useif-shellorrun-shellin~/.tmux.conf, the initial session (and any sessions, windows, or panes created explicitly created through~/tmux.conf) will lack any tmux configuration arranged throughif-shellorrun-shellcommands.Chris Johnsen– Chris Johnsen2013-01-22 03:01:42 +00:00Commented Jan 22, 2013 at 3:01 - 1@ChrisJohnsen
if-shellworks as expected for me with tmux 1.8. I am using it to set set-titles-string only for SSH: github.com/blueyed/dotfiles/commit/…blueyed– blueyed2014-02-19 13:59:20 +00:00Commented Feb 19, 2014 at 13:59 - 3This should be accepted; it's the proper way to do it.ChevCast– ChevCast2014-11-14 18:06:15 +00:00Commented Nov 14, 2014 at 18:06
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 The if-shell check must be in a string and the command to run must be enclosed in {}. Here is an example of how to bind the ] key to paste the system clipboard, with a command that varies depending on the operating system.
if-shell 'uname | grep -q Linux' { bind-key ] run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer" } if-shell 'uname | grep -q Darwin' { bind ] run "reattach-to-user-namespace pbpaste | tmux load-buffer - && tmux paste-buffer" } - 1Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.2023-03-13 06:05:30 +00:00Commented Mar 13, 2023 at 6:05