如何更改tmux会话的起始目录?

btt*_*ttf 26 unix linux tmux

启动tmux会话的目录将是所有新窗口将从其开始的目录.

我的问题是,如何在不关闭会话的情况下更改此起始目录?

chu*_*ash 29

执行此操作的方法是从会话中分离(^b d使用默认的键绑定),然后在重新连接时指定其他目录.附加到会话时,使用该-c标志指定工作目录.这是一个例子:

$ 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
Run Code Online (Sandbox Code Playgroud)

此设置将保留 - 在您重置工作目录后,每次重新连接到会话时都不需要继续指定它.

为了记录,我在tmux版本2.0(虽然我认为不重要 - 我找不到任何关于在更改日志中添加命令的-c选项,attach-session所以我认为它已经存在了很长一段时间).

  • 这在开始新会话时也有效。`tmux new -s foo -c ~/some/path`。我的是2.1版本。 (2认同)

spa*_*azm 12

attach-session -c从命令提示符或绑定中使用。

我有使用提供的 var 自动使用当前目录的绑定pane_current_path

我将其绑定到M-c, 以更新当前路径。

# tmux.conf
# set default directory for new windows in this session to current directory:
bind M-c attach-session -c "#{pane_current_path}"
Run Code Online (Sandbox Code Playgroud)

同样,当我只想要当前目录中的另一个窗口而不更改默认值时,我有一个绑定C

# tmux.conf
# open a new window in the current directory
bind C new-window -c "#{pane_current_path}"
Run Code Online (Sandbox Code Playgroud)

我的绑定:

  • c:在会话默认目录中打开一个新窗口[默认绑定]
  • C:在当前目录下打开一个新窗口
  • M-c:将会话默认目录设置为当前目录

快乐 Tmuxing!


Ang*_*elo 9

Chucksmash的答案是一个很好的答案,但是如果您愿意,也可以不使用会话就可以实现。该命令attach-session在tmux命令提示符下也可用。并且可以使用点将目标会话指定为“当前”会话。

attach-session -t . -c /path/to/new/directory
Run Code Online (Sandbox Code Playgroud)

  • 您也可以在会话中执行以下操作。`:attach-session -t 。-c '#{pane_current_path}'` (7认同)
  • 不需要 `-t .`。`attach-session -c '#{pane_current_path}'` 就足够了。 (3认同)
  • 这允许添加键绑定来更新工作目录,例如使用“bind -n Mu Attach-session -t”。-c '#{pane_current_path}'` 将 alt-u (无前缀)绑定到此类更新。 (2认同)

wis*_*cky 5

下面是如何在不分离会话的情况下更改 tmux 会话的工作目录,并且不需要使用<prefix>按键:

(选项 1)tmux命令提示符下输入目录:

tmux command-prompt "attach -c %1"

...将打开命令提示符,然后键入所需的工作目录~/my/dir并按ENTER

(选项 2)在窗格内命令行上提供目录:

# Execute this in one of the shell panes of within your tmux session:
tmux command-prompt -I $PWD -P "New session dir:" "attach -c %1"
Run Code Online (Sandbox Code Playgroud)

使用这种方法,new-directory 的提示预先填充了启动命令的窗格的当前目录。当然,$PWD如果你愿意,你可以用其他任何东西代替。

想要一个shell函数?

我已将此添加到我的 shell 初始化中:

# Change the current directory for a tmux session, which determines
# the starting dir for new windows/panes:
function tmux-cwd {
    tmux command-prompt -I $PWD -P "New session dir:" "attach -c %1"
 }
Run Code Online (Sandbox Code Playgroud)

使用所有这些选项,任何未来的新窗口都将在给定的目录中启动。

注意: attachattach-sessiona都是彼此的别名。将tmux command-prompt有许多其他的权力,这是值得阅读的手册页