如何自动将tmux窗口重命名为当前目录

aha*_*s88 41 tmux

我想让tmux自动使用当前工作目录(cwd)重命名窗口.默认情况下,它将选项卡/窗口命名为当前进程的名称,例如zsh或vim.

当我在tmux中打开一个新窗口时,名称是reattach-to-use-namespace,然后立即切换到zsh.

tmux标签

我在OS X 10.10.2上,我使用zshell,我有tmux 1.9a.

要清楚,我不希望窗口名称中的整个路径,只是当前目录,所以例如,我想要projectName,而不是/Users/username/Development/projectName.

如果你想看到我的当前tmux.conf,这里是.

CEL*_*CEL 24

扩展Josef所写的内容,您可以使用shell片段将目录的基本名称置于状态:

# be sure to see note* below
set -g window-status-format '#I:#(pwd="#{pane_current_path}"; echo ${pwd####*/})#F'
set -g window-status-current-format '#I:#(pwd="#{pane_current_path}"; echo ${pwd####*/})#F'

# status bar updates every 15s by default**, change to 1s here 
# (this step is optional - a lower latency might have negative battery/cpu usage impacts)
set -g status-interval 1
Run Code Online (Sandbox Code Playgroud)

*请注意,这将是${pwd##*/}被逃脱了${pwd####*/},因为#在格式字符串特殊的意义.

**有关默认tmux配置的示例,请参见此处.

  • 实际上,这不会设置窗口名称,而只会设置状态.使用`tmux list-windows`进行检查. (3认同)

Jus*_*yes 24

使用tmux 2.3+,b:格式修饰符显示路径的"basename"(或"tail").

set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#{b:pane_current_path}'
Run Code Online (Sandbox Code Playgroud)

FORMATS部分man tmux描述了其他修饰符,例如#{d:}甚至#{s/foo/bar/:}.


使用tmux 2.2或更早版本时,basename可以使用shell命令.

set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#(basename "#{pane_current_path}")'
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法获取“~”而不是完整的用户名? (3认同)
  • @Toothrot 当然!`#{b;s/your_username/~/:pane_current_path}` 是正确的方法 (3认同)

Gre*_*ell 15

为了充分利用这两个世界 - 当您处于 shell 提示符时,窗口名称是路径,但当您运行某些东西时,窗口名称是可执行文件的名称,请尝试以下操作:

set-option -g status-interval 1
set-option -g automatic-rename on
set-option -g automatic-rename-format "#{?#{==:#{pane_current_command},bash},#{b:pane_current_path},#{pane_current_command}}"
Run Code Online (Sandbox Code Playgroud)

将“bash”替换为您正在使用的任何 shell。


Cir*_*四事件 13

显示前N个组件

在此输入图像描述

仅显示基本名称会产生太多歧义,但完整路径太杂乱,所以我决心:

the/last/path
Run Code Online (Sandbox Code Playgroud)

代替:

/a/very/long/the/last/path
Run Code Online (Sandbox Code Playgroud)

要不就:

path
Run Code Online (Sandbox Code Playgroud)

.tmux.conf:

set-window-option -g window-status-current-format '#[fg=white,bold]** #{window_index} #[fg=green]#{pane_current_command} #[fg=blue]#(echo "#{pane_current_path}" | rev | cut -d'/' -f-3 | rev) #[fg=white]**|'
set-window-option -g window-status-format '#[fg=white,bold]#{window_index} #[fg=green]#{pane_current_command} #[fg=blue]#(echo "#{pane_current_path}" | rev | cut -d'/' -f-3 | rev) #[fg=white]|'
Run Code Online (Sandbox Code Playgroud)

诀窍取自:Unix删除部分路径

如果仍然没有解决歧义,我会去:

bind-key -r w choose-window -F '#{window_index} | #{pane_current_command} | #{host} | #{pane_current_path}'
Run Code Online (Sandbox Code Playgroud)

在Tmux 2.1,Ubuntu 16.04上测试.