Shell Tmux将窗口重命名为当前目录

Shell Tmux将窗口重命名为当前目录,shell,tmux,Shell,Tmux,因此,我在.zshrc中有一个函数,用于将当前tmux窗口重命名为当前目录 precmd () { tmux set-window-option -qg window-status-format "#[fg=colour18 bg=colour18] | #[fg=white, bg=colour18] #I | ${PWD##/*/} #[fg=colour18, bg=colour18] | " tmux set-window-option -qg window-status

因此,我在
.zshrc
中有一个函数,用于将当前tmux窗口重命名为当前目录

precmd () {
    tmux set-window-option -qg window-status-format "#[fg=colour18 bg=colour18] | #[fg=white, bg=colour18] #I | ${PWD##/*/} #[fg=colour18, bg=colour18] | "
    tmux set-window-option -qg window-status-current-format "#[fg=colour18, bg=blue] | #[fg=white, bg=blue] #I | ${PWD##/*/} #[fg=blue, bg=colour18] | "
}
这很好,但我想知道为什么相同的代码在我的
.tmux.conf

# Status Bar
set-window-option -qg window-status-format "#[fg=colour18 bg=colour18] | #[fg=white, bg=colour18] #I | ${PWD##/*/} #[fg=colour18, bg=colour18] | "

set-window-option -qg window-status-current-format "#[fg=colour18, bg=blue] | #[fg=white, bg=blue] #I | ${PWD##/*/} #[fg=blue, bg=colour18] | "
如果我尝试使用它,我会在启动tmux时出错。
有什么原因吗?使用
{pane\u current\u path}可以获得相同的效果吗“
并将路径缩短到当前目录

我认为它不起作用,因为函数是在创建tty之前执行的。它与shell函数一起工作,因为您在tmux完全启动后执行它。但是
.tmux.conf
是在tmux启动期间执行的


因此,我会将其保存在您的
.zshrc
中,可能会立即执行该函数。

目前恐怕没有简单的方法来实现这一点。除了@rednaw所说的之外,还有另一个重要的因素使得它不可能实现

tmux支持“#(shell命令)”的语法。因此,你可能很想

tmux set-window-option window-status-format "#(echo ${PWD##/*/})"

但是,这一行不会像您预期的那样工作。棘手的事情是:“PWD”是当前会话的PWD,而不是当前窗格的PWD。

尽管shell命令在会话的工作目录(而不是窗口或窗格)中运行,因此您不能使用
$PWD
,但您仍然可以使用
{pane\u current\u path}
从该命令中获取窗格的工作路径,然后使用shell命令将其缩短为路径的最后一部分,例如,我的tmux配置中有:

tmux_conf_theme_window_status_format='#I #(tmux_pwd="#{pane_current_path}"; echo "${tmux_pwd//*\//}"): #W'
tmux_conf_theme_window_status_current_format='(#I) #(tmux_pwd="#{pane_current_path}"; echo "${tmux_pwd//*\//}"): #W'

您在tmux中遇到的错误是什么<代码>无效或未知的命令谢谢,这是我开始思考的。你可以用这个shell命令来代替
#(tmux\u pwd=“#{pane\u current\u path}”echo“${tmux\u pwd/*\/}”)
-只是作为另一个答案添加的。