82

I recently moved from GNU screen to tmux.
I find it quite similar but with bigger support (I switched due to problem with escape-time in neovim- resolution was only for tmux).

Unfortunately in tmux I'm unable to find a similar command to this:

screen -X eval "chdir $(some_dir)"

The command above changed the default directory for new window/screen/pane from within the GNU screen so when I pressed Ctrl+a (similar to tmux Ctrl+b)- new window opened in the $(some_dir) directory.

Is there a similar thing in tmux?

ANSWER:
I have used @Lqueryvg answer and combined it with @Vincent Nivoliers suggestion froma a comment and that gave me a new binding for a command attach -c "#{pane_current_path}" which sets my current directory as a default one.
Thanks.

lewiatan
  • 1,129
  • You could use the second answer of this question – Vincent Nivoliers Mar 08 '16 at 13:25
  • Thanks, it is helpful. new-window -c "#{pane_current_path}" is working but what I need is to set the path permanently, i.e.: I'm working in a project in /aaa/bbb but somehow I when I change path to say /ccc/ddd and start new with C-b and c I want to land one more time in /aaa/bbb. Can you think of a solution to this? – lewiatan Mar 08 '16 at 13:41
  • I am personally fine with the addition of the last three lines in my .tmux.conf. That way I only have to navigate once to the folder and then create my windows / split from there, but I admit this is not a solution to your problem, that's why I didn't post an answer ! – Vincent Nivoliers Mar 08 '16 at 13:52
  • @lewiatan Mind editing the one-line answer attach -c "#{pane_current_path}" into Lqueryvg's answer? I almost missed your comment down there and definitely your edit up here ^^' – nuala Aug 28 '20 at 23:41
  • 1
    @nuala I'm not sure what do you want me to do. Feel free to edit my post and/or the answer if you can improve it – lewiatan Aug 31 '20 at 20:29

2 Answers2

126

tl;dr

  • Ctrl+b :

  • attach -c desired/directory/path

Long Answer

  1. Start tmux as follows:

     (cd /aaa/bbb; tmux)
    

Now, any new windows (or panes) you create will start in directory /aaa/bbb, regardless of the current directory of the current pane.

  1. If you want to change the default directory once tmux is up and running, use attach-session with -c.

Quoting from the tmux man page for attach-session:

    -c will set the session working directory (used for new windows)
    to working-directory.

For example:

  • Ctrl+b :

  • attach -c /ddd/eee

New windows (or panes) will now start in directory /ddd/eee, regardless of the directory of the current pane.

KFL
  • 269
Lqueryvg
  • 1,979
7

For those who might be looking for a bit more reference than the selected answer provides, there was an another good answer to this question over on StackOverflow:

https://stackoverflow.com/questions/27307815/how-to-change-the-starting-directory-of-a-tmux-session

Which provides ways you can do it without leaving the session, and for those who use tmux in a multi-session fashion (like myself), the above answer gives a bit better context as to how attach/attach-session works.

See both available answers for different ways they can be used. I found both to be helpful/insightful.