tmux 另開視窗繼承工作目錄
以前沒有特別注意到的功能,Aaron 說 tmux 更新成 1.9 就不會動了,官方 FAQ - How can I open a new window in the same directory as the current window? 提供了一個方法,但是設上去了也沒有效。
官方的方法 TMUXPWD_$(tmux display -p "#I")
是把當下的工作目錄存在環境變數,#I
為 Index of Window,然後 bind 一組按鍵來執行 tmux new-window
指令,可以簡寫成 neww
,該指令後面可以帶 shell-command,如此,就可以在開新視窗時切換到預先存在環境變數的工作目錄。
這個麻煩的方法不能動的原因在於 $PWD 的 precedence,其實可以用 PROMPT_COMMAND
來修正,不過緊接著提供的方法更簡潔。
PROMPT_COMMAND=$([ -n "$TMUX" ] && tmux setenv TMUXPWD_$(tmux display -p "#I") $PWD)
tmux 1.9 之前是照著下面這個 commit 的說明運作,default-path
設定來決定另開新窗的預設工作目錄,如果沒有設定,預設就是繼承上一個視窗的工作目錄。
c1b994852594b23b7443e01e05257c991684ba4e -p
commit c1b994852594b23b7443e01e05257c991684ba4e
Author: Nicholas Marriott <nicholas.marriott@gmail.com>
Date: Fri Dec 9 16:37:29 2011 +0000
Change the way the working directory for new processes is discovered. If
default-path isn't empty, it is used. Otherwise:
1) If tmux neww is run from the command line, the working directory of the
client is used.
2) Otherwise use some platform specific code to retrieve the current working
directory of the process in the active pane.
3) If that fails, the directory where the session was created is used.
Idea and support code, Linux, Solaris, FreeBSD bits by Romain Francoise,
OpenBSD bits by me.
不過 default-path
在 1.9 時被移除了,見下面 commit,取而帶之是用 -c
的參數來決定 new
、neww
、splitw
開新視窗時的工作目錄。
commit 4538c269d0b366a770a5a5ebfe0c5007569edbc1
Author: Nicholas Marriott <nicholas.marriott@gmail.com>
Date: Sun Oct 6 21:02:23 2013 +0100
Alter how tmux handles the working directory to internally use file descriptors
rather than strings.
- Each session still has a current working directory.
- New sessions still get their working directory from the client that created
them or its attached session if any.
- New windows are created by default in the session working directory.
- The -c flag to new, neww, splitw allows the working directory to be
overridden.
- The -c flag to attach let's the session working directory be changed.
- The default-path option has been removed.
To get the equivalent to default-path '.', do:
bind c neww -c $PWD
To get the equivalent of default-path '', do:
bind c neww -c '#{pane_current_path}'
The equivalent of default-path '~' is left as an exercise for the reader.
This also changes the client identify protocol to be a set of messages rather
than one as well as some other changes that should make it easier to make
backwards-compatible protocol changes in future.
最後我們把 escape + CTRL-c 的組合鍵定義成開新視窗時保留當下的工作目錄的動作。
// .tmux.conf
bind-key C-c new-window -c '#{pane_current_path}'
有沒有注意到些什麼? tmux commit message 寫的好漂亮...
Happy ending.