Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Tmux from dereferencing path when creating new window/pane

Tags:

symlink

tmux

When I create new window/pane in tmux, for example via tmux neww or keybindings prefix+c, prefix+% etc, the new pane gets working directory the same as previous pane, but with dereferenced symbolic links in path.

For example, if I am at

/home/user/my-link/a

where my-link -> /mnt/user/, i got to

/mnt/user/a

Explicitly passing new directory to tmux does not work either:

tmux neww -c $(pwd)

Can I disable such dereferencing? I think I can write a workaround via tmux environment variables, but I want a clearer solution.

I am running tmux 1.8 from repos on Ubuntu 14.04.

like image 644
Lapshin Dmitry Avatar asked Oct 03 '15 13:10

Lapshin Dmitry


3 Answers

This behavior cannot be disabled, and it seems it is not even possible to implement this feature in tmux (source).

In Linux the working directory of a process is always tracked as the actual directory (with symlinks resolved). You can see this by issuing ls -l /proc/self/cwd in the directory /home/user/my-link/a, it will show that the current working directory is actually /mnt/user/a. The reason for this is probably for not running into trouble when the symlink is deleted (or even changed) while a process is in that directory.

The feature that your shell shows you /home/user/my-link/a as working directory is implemented completely in the shell itself. It keeps track of it in the pwd environment variable, but tmux cannot access environment variables of subprocesses.

The easiest way I have found to create a new window as you would like to is

tmux neww "cd $(pwd); exec $SHELL"
like image 184
Philipp Wendler Avatar answered Sep 28 '22 04:09

Philipp Wendler


In addition to Philipp's answer, there's a way to work around the runtime substitution problem. You just need to take care that every time you change the directory the global tmux variable PWD is updated with $PWD. In your .bashrc:

mycd() {
  \cd "$@"
  [ -n "$TMUX" ] && tmux set-environment -g PWD $PWD
}
alias cd=mycd

In your .tmux.conf:

bind-key C-n new-window
like image 40
yacc Avatar answered Sep 28 '22 05:09

yacc


Summaring all answers and comments from Philipp Wendler and Yacc, I came up with solution that works perfectly for me. I should mension that it uses some tricks on my machine, so they should be used carefully.

It was shown that tmux can't solve problem on it's own, we need some tricks.

First, make that every pane describe it's path in tmux variable TMUX_<pane-id>_PATH. That could be done via aliasing cd or prompt code (I am using that), it doesn't matter:

# get $pane set to pane id without %
tmux set-environment TMUX_"$pane"_PATH $(pwd)

Second, have in path script tmux-neww.sh. It sets NEWW variable to current real path's way. It gets current pane-id as a param:

#!/bin/bash
pane=$(echo "$1" | tr -d '%')
pane_path=$(tmux show-environment TMUX_"$pane"_PATH | sed 's/^[^=]*=//g')
tmux set-environment NEWW "$pane_path"
tmux neww

Third, in tmux.conf:

bind C \
    run "tmux-neww.sh #{pane_id}"

Forth, I have in bash.bashrc test, if shell is being run in tmux. If so, it makes some changes (i.e. adds some variables that my heavy prompt will send some data to tmux variables). Here it tests, if NEWW is set:

neww=$(tmux show-environment NEWW 2> /dev/null | sed 's/^[^=]*=//')
if [ "$neww" != "-NEWW" ] && [ "$neww" != "" ] ; then
    cd "$neww"
fi
tmux set-environment -r NEWW

That may be overwhelming, but works OK.

like image 35
Lapshin Dmitry Avatar answered Sep 28 '22 03:09

Lapshin Dmitry