Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to execute an external command based on the value of my session name. I know that the :display-message '#S' will give me the session name, but I want to have that name automatically sent to my external command.

I've tried :run-shell "my-command #S", but it does not work, as "#S" is not substituted.

Is there anything like :MY_VAR=$(display-message -p "#S") that I could use?

share|improve this question

2 Answers 2

Since you can run almost all tmux commands directly from a shell (not just from a tmux : prompt), you can do this:

:run-shell "my-command \"$(tmux display -p '#S')\""

Of course this can also be done from a binding:

:bind C run-shell "my-command \"$(tmux display -p '#S')\""
share|improve this answer
    
But if I have two different sessions open, it will print the name of the session that's currently in focus, not necessarily the session that spawned run-shell... –  src Aug 9 '12 at 18:37
    
Right, the environment created for the commands started by run-shell is the “global” environment, so “the current session” will always refer to the most recently active session (because the last component of the value of the TMUX environment variable is -1 instead of an (internal) session index). If you want to run a command inside the context of a session, then maybe you should use a (transient) detached window to run your command: new-window -d 'my-command "$(tmux display -p "#S")"' (or write my-command to do the tmux display -p '#S' part itself instead of its parent shell doing it). –  Chris Johnsen Aug 10 '12 at 3:52

If you are running tmux 1.8 (you can check with tmux -V), you can simply use :run-shell "my-command '#S'". This doesn't work in 1.6, though.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.