1

I have been using tmux for a few months, and there is one thing that always bothered me: when I type cd into my shell, I get moved into my home directory (this is the standard behavior of cd, so nothing unexpected).
What I would like to have is that I get into the current tmux session's base directory, and this could probably easily be implemented via a shell function:

cd () { if [ $# -eq 0 ] then command cd $TMUX_BASE_DIR # fictional environment variable else command cd $@ } 

However, I have not found a way to find this directory. tmux does not have a build-in command apparently nor sets an environment variable. I also skimmed over the man page, but didn't find anything in the COMMANDS or VARIABLES section. This directory needs to be saved somewhere though, as opening a new tmux window makes it go to this directory.
Of course one I open a new window and close one everytime I want to achieve the desired behavior, but this is surely less than optimal and there needs to be a better way.

Is there a way to achieve this behavior / get the session base directory in tmux? If so, how?

1 Answer 1

3

My tmux(1) manual documents a session_path, so maybe you could save it with something like this "tmux path session report" script:

#!/bin/sh TMPFILE=`mktemp /tmp/tps-report.XXXXXXXXXX` || exit 1 tmux run "printf '#{session_path}\n' > \"$TMPFILE\"" cat "$TMPFILE" rm $TMPFILE 

Also your $@ probably should be written as "$@".

2
  • When I execute tmux run "printf '#{session_path}\n', it outputs a single n and exits. Also my tmux(1) manual doesn't document session_path at all, I'll maybe look if it's an old version. Commented Nov 12, 2022 at 16:15
  • My tmux version is 3.0, which seems to be the newest version available in apt. The current version on GitHub is 3.3, so I should probably take a look at building it from source. Commented Nov 12, 2022 at 16:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.