Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Has there been any progress on this? tmux 1.9 still does not expand #(/bin/echo "#S"). Instead it passes "#S", not the acutal session name – Michael Krupp May 6 '15 at 18:50

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

You could use sed + xargs to pass the result as a parameter to your desired command, eg:

tmux bind-key "C" run-shell "tmux display-message -p '#{S}' | sed 's/.*/\"&\"/' | xargs my_command"
share|improve this answer

You must log in to answer this question.

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