# Get the wrong tmux session when connecting kakoune to a given session

**URL:** <https://discuss.kakoune.com/t/get-the-wrong-tmux-session-when-connecting-kakoune-to-a-given-session/1791>\
**Category:** Help\
**Tags:** session, tmux\
**Created:** [June 22, 2021, 10:23pm UTC](https://discuss.kakoune.com/t/get-the-wrong-tmux-session-when-connecting-kakoune-to-a-given-session/1791 "2021-06-22T22:23:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/scr/32/486_2.png) [@scr](https://discuss.kakoune.com/u/scr)\
**Post date:** [June 22, 2021, 10:23pm UTC](https://discuss.kakoune.com/t/get-the-wrong-tmux-session-when-connecting-kakoune-to-a-given-session/1791/1 "2021-06-22T22:23:49Z")

</div>

Hello,

Let’s say that I start a kakoune session named “k1” in a tmux session named “t1”.

Then I open a new terminal with a new tmux session “t2”, and start a kakoune client connect to “k1” ( `kak -c k1`)

Now if I want to get the name of the current tmux session from my connected kakoune client ( running `%sh{ tmux display-message -p '#S' }` ) I don’t get “t2” but “t1” instead.

How can I get the tmux session in which the client is running, instead of the tmux session in which the kakoune session is running?

---

<div class="post-metadata">

**Author:** ![eraserhd](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/eraserhd/32/15_2.png) [@eraserhd](https://discuss.kakoune.com/u/eraserhd)\
**Post date:** [June 23, 2021, 3:33am UTC](https://discuss.kakoune.com/t/get-the-wrong-tmux-session-when-connecting-kakoune-to-a-given-session/1791/2 "2021-06-23T03:33:41Z")

</div>

Since tmux communicates the active session using environment variables, you can re-export the client versions for the command (which runs on the server):

echo %sh{TMUX\_SESSION=“$kak\_client\_env\_TMUX\_SESSION” tmux display-message -p ‘#S’}

---

<div class="post-metadata">

**Author:** ![scr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/scr/32/486_2.png) [@scr](https://discuss.kakoune.com/u/scr)\
**Post date:** [June 23, 2021, 11:30am UTC](https://discuss.kakoune.com/t/get-the-wrong-tmux-session-when-connecting-kakoune-to-a-given-session/1791/3 "2021-06-23T11:30:41Z")

</div>

Which tmux version are you running ?  
I think TMUX\_SESSION doesn’t exist with my version of tmux ( I’m running 2.8 )

---

<div class="post-metadata">

**Author:** ![scr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/scr/32/486_2.png) [@scr](https://discuss.kakoune.com/u/scr)\
**Post date:** [June 23, 2021, 11:39am UTC](https://discuss.kakoune.com/t/get-the-wrong-tmux-session-when-connecting-kakoune-to-a-given-session/1791/4 "2021-06-23T11:39:40Z")

</div>

I have just used `tmux_session=$(echo "$kak_client_env_TMUX" | rev | cut -d',' -f1 | rev)` and it work well for me,  
Thanks a lot !

---

<div class="post-metadata">

**Author:** ![eraserhd](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/eraserhd/32/15_2.png) [@eraserhd](https://discuss.kakoune.com/u/eraserhd)\
**Post date:** [June 23, 2021, 1:17pm UTC](https://discuss.kakoune.com/t/get-the-wrong-tmux-session-when-connecting-kakoune-to-a-given-session/1791/5 "2021-06-23T13:17:51Z")

</div>

I was answering from my iPad and the environment variable name was just wrong. The full command should be like so:

```
echo %sh{
   TMUX="$kak_client_env_TMUX" \
       TMUX_PANE="$kak_client_env_TMUX_PANE" \
       tmux display-message -p '#S'
 }

```

While you can get the session from the TMUX variable yourself, I would recommend leaving its contents as a “black box” for tmux to parse, since it is not documented. with the above trick, you can run _any_ tmux command as though it were in the client, not just getting the session. (`$TMUX_PANE` is not required for the above command, but why not?)

---

<div class="post-metadata">

**Author:** ![scr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/scr/32/486_2.png) [@scr](https://discuss.kakoune.com/u/scr)\
**Post date:** [June 23, 2021, 1:37pm UTC](https://discuss.kakoune.com/t/get-the-wrong-tmux-session-when-connecting-kakoune-to-a-given-session/1791/6 "2021-06-23T13:37:31Z")

</div>

That solution doesn’t work for me, which is sad because it is more elegant than the dirty session parsing, but I guess I’ll stick with that for now 😕
