How to Change the Starting Directory of a Tmux Session

How do I change the starting directory of a tmux session?

The way to do this is to detach from the session (^b d with the default keybindings) and then specify a different directory when you reattach to it. When attaching to a session, use the -c flag to specify the working directory. Here's an example:

$ tmux list-sessions
tmuxwtfbbq: 3 windows (created Tue Apr 5 14:25:48 2016) [190x49]
$ tmux attach-session -t tmuxwtfbbq -c /home/chuck/new_default_directory

This setting will be persisted - after you've reset the working directory, you won't need to keep specifying it every time you reattach to the session.

For the record, I'm on tmux version 2.0 (though I don't think it matters - I couldn't find anything about adding a -c option to the attach-session command in the change logs so I assume it's been there for quite a while).

Different starting directory per window?

This question is very similar to: https://unix.stackexchange.com/questions/12032/create-new-window-with-current-directory-in-tmux

It depends on your tmux version but the -c parameter does do the trick but it does not remember the setting. There used to be a default-path setting but that has been removed in version 1.9 unfortunately.

For newer versions you will need to pass along the -c in all cases (you can use an alias if you manually execute that command) or if you use key bindings you need to rebind the split/new window keys.

bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"

To use a custom path instead of the current pane path, execute this command:

tmux setenv custom_path /home/whatever/some/path

Put this in your config:

bind '"' split-window -c "#{custom_path}"
bind % split-window -h -c "#{custom_path}"
bind c new-window -c "#{custom_path}"

Create new tmux session inside a session with default path and name

From the man page:

new-session [-AdDEP] [-c start-directory] [-F format] [-n window-name] [-s session-name] [-t group-name] [-x width] [-y height] [shell-command] (alias: new) Create a new session with name session-name.

If I understand your question correctly, you are looking for the -s and -c options.

Changing the location where tmux saves sessions?

By default tmux does not save its session. You need a plugin like tmuxinator or tmux-resurrect.

If you use tmux-resurrect plugin to save your session. It saves by default into ~/.tmux/resurrect directory. You can customize the location by adding this to your .tmux.conf

set -g @resurrect-dir '/some/path'

tmux-resurrect can be combined with tmux-continuum to add auto-save and restore features.

Features:

  • continuous saving of tmux environment
  • automatic tmux start when computer/server is turned on
  • automatic restore when tmux is started

How to start tmux with several windows in different directories?

The shell errors are probably due to some problem in your startup files (or something they run).

As shellter commented, temporarily including the command set -vx early in your startup sequence is a good way to find out where the errors are occurring.

If you find the -vx output too verbose, you could try “printf debugging” (manually adding debug statements to your startup files until you can narrow down exactly which lines are causing the errors):

  • Put echo start of .bashrc and echo end of .bashrc at the start/end of your .bashrc to see if the error occurs during your .bashrc. If not, instrument your other startup files: .bash_profile/.bash_login/.profile. If the errors happen before that file, then the problem may be in /etc/profile.
  • Once you know which file is being processed when the errors occur, add more debug outputs around each “major block” or line to narrow down the responsible section/line.
  • The errors may not actually be in your startup file itself, but in a script that it runs.

Note: These debug additions need to be temporary since they will cause problems if you ever use a program that makes automated logins (e.g. rsync, SSH-based Git access, etc.) since these programs expect a “clean” connection without such debugging noise present.


There should be no need to use cd command like that in the shell-command argument given to either tmux new-session or tmux new-window.

A new window will “inherit” the current working directory when using new-session and new-window from the command line (i.e. when done through the tmux binary, instead of via a binding or at a tmux-: prompt). According to the CHANGES file, it looks like this has been the case since tmux 0.6 (at least for new-window).

This is tmux-mediated inheritance, not the parent–child inheritance that is the usual mechanism for passing along the cwd.

This script works for me with tmux 1.5:

#!/bin/bash
# var for session name (to avoid repeated occurences)
sn=xyz

# Start the session and window 0 in /etc
# This will also be the default cwd for new windows created
# via a binding unless overridden with default-path.
cd /etc
tmux new-session -s "$sn" -n etc -d

# Create a bunch of windows in /var/log
cd /var/log
for i in {1..6}; do
tmux new-window -t "$sn:$i" -n "var$i"
done

# Set the default cwd for new windows (optional, otherwise defaults to session cwd)
#tmux set-option default-path /

# Select window #1 and attach to the session
tmux select-window -t "$sn:1"
tmux -2 attach-session -t "$sn"

This might also (as a side-effect) alleviate your shell startup errors since the way tmux starts a shell is different from a plain bash -i (it is more akin to bash -l, which uses your .bash_profile/.bash_login/.profile instead of (just) your .bashrc).

Tmux: How to configure tmux to display the current working directory of a pane on the status bar?

There is a variable for that, which doesn't seem to be in the manpage but is mentioned in the development version. For me, it works in the 1.8 release of tmux.

set -g status-left "#{pane_current_path}"

Note that it also works when you put it in the window-status. Each window status will mention respective working directories.

setw -g window-status-format "#{pane_current_path}".


Related Topics



Leave a reply



Submit