如何自动重命名 tmux windows 到当前目录
How to automatically rename tmux windows to the current directory
我想让 tmux 自动用当前工作目录 (cwd) 重命名 window。默认情况下,它将 tab/window 命名为当前进程的名称,例如 zsh 或 vim.
当我在 tmux 中打开一个新的 window 时,名称是 reattach-to-use-namespace
然后它立即切换到 zsh
.
我在 OS X 10.10.2,我使用 zshell,我有 tmux 1.9a。
需要说明的是,我不想要 window 名称中的整个路径,只是当前目录,例如,我想要 projectName
,而不是 /Users/username/Development/projectName
.
如果你想看我现在的tmux.conf
,here it is.
在 zsh 的 tmux 会话中做这样的事情 shell:
setopt PROMPT_SUBST
export PS1=$'\ek$(basename $(pwd))\e\> '
如果有人使用 bash shell:
export PS1="3k$(basename $(pwd))3\> "
您可以在 shell 初始化文件中添加这些命令,条件是 $TERM
env 变量设置为值 "screen"
将此配置添加到您的 ~/.tmux.conf 文件应该有效:
set-option -g window-status-current-format '#I:#{pane_current_path}#F'
set-option -g window-status-format '#I:#{pane_current_path}#F'
set-option -g status-interval 1
但这取决于您的 Tmux 版本。我无法让它在 1.9a3(在 Cygwin 中)上工作 - 但是在 Ubuntu(在 Vagrant 中)上使用 Tmux 1.8 它工作正常。
扩展 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
*请注意,${pwd##*/}
被转义为 ${pwd####*/}
,因为 #
在格式字符串中具有特殊含义。
**有关默认 tmux 配置示例,请参阅 here。
显示前 N 个组件
只显示基本名称会产生太多歧义,但完整路径会太混乱,所以我选择了:
the/last/path
而不是:
/a/very/long/the/last/path
或者只是:
path
.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]|'
技巧取自:Remove part of path on Unix
如果仍然不能解决歧义,我继续:
bind-key -r w choose-window -F '#{window_index} | #{pane_current_command} | #{host} | #{pane_current_path}'
在 Tmux 2.1、Ubuntu 16.04 上测试。
我正在为此使用 zsh 钩子
在~/.zshrc
中添加以下内容
precmd () {
if [ -n "$TMUX" ]; then
tmux set-window-option -q window-status-format "#[fg=cyan bg=cyan] | #[fg=white, bg=cyan] #I | ${PWD##/*/} #[fg=cyan, bg=cyan] | "
tmux set-window-option -q window-status-current-format "#[fg=cyan, bg=cyan] | #[fg=white, bg=cyan] #I | ${PWD##/*/} #[fg=cyan, bg=cyan] | "
fi
}
我在 ~/.tmux.conf 中使用以下内容来实现此目的(在 OSX、zsh、tmux-2.3 上工作):
set -g automatic-rename-format '#{pane_current_path}'
set -g status-interval 5
您可以将 status-interval 设置为 1 以使其更快地响应更改目录。
根据变更日志 (https://raw.githubusercontent.com/tmux/tmux/master/CHANGES) 这应该适用于 tmux 1.9 及更高版本。
在装有 tmux 2.3 的 CentOS 机器上使用 ssh window 名称不会改变,直到我在新面板中按下 return,不知道为什么会这样。
这并没有严格回答您的问题——它不会自动将现有的 tmux 会话重命名为当前工作目录。
相反,在创建新会话时,它会以当前工作目录命名该会话。
这是我所做的:
到
~/.aliases
添加
alias tm='tmux new -s `basename $PWD`'
打开一个新终端 window 并输入:
tm
现在这将创建一个新的 tmux 会话,该会话以当前工作目录命名。
注意:这依赖于 basename
,Windows 中不存在。
使用 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}'
man tmux
的 FORMATS 部分描述了其他修饰符,例如 #{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}")'
我确定你想使用这个:
set -g status-left '#{pane_current_path} '
要更改您在 window 列表中看到的内容,您可以在为 chose-window
函数定义键绑定时指定一种格式,如下所示:
bind-key '"' choose-window -F "#{session_name} | #{window_name} - #{b:pane_current_path} (#{pane_current_command})"
为了两全其美 - 当你在 shell 提示时,window 名称是路径,但当你在 运行 时,名称是可执行文件的名称,请尝试这个:
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}}"
将“bash”替换为您正在使用的 shell。
我想让 tmux 自动用当前工作目录 (cwd) 重命名 window。默认情况下,它将 tab/window 命名为当前进程的名称,例如 zsh 或 vim.
当我在 tmux 中打开一个新的 window 时,名称是 reattach-to-use-namespace
然后它立即切换到 zsh
.
我在 OS X 10.10.2,我使用 zshell,我有 tmux 1.9a。
需要说明的是,我不想要 window 名称中的整个路径,只是当前目录,例如,我想要 projectName
,而不是 /Users/username/Development/projectName
.
如果你想看我现在的tmux.conf
,here it is.
在 zsh 的 tmux 会话中做这样的事情 shell:
setopt PROMPT_SUBST
export PS1=$'\ek$(basename $(pwd))\e\> '
如果有人使用 bash shell:
export PS1="3k$(basename $(pwd))3\> "
您可以在 shell 初始化文件中添加这些命令,条件是 $TERM
env 变量设置为值 "screen"
将此配置添加到您的 ~/.tmux.conf 文件应该有效:
set-option -g window-status-current-format '#I:#{pane_current_path}#F'
set-option -g window-status-format '#I:#{pane_current_path}#F'
set-option -g status-interval 1
但这取决于您的 Tmux 版本。我无法让它在 1.9a3(在 Cygwin 中)上工作 - 但是在 Ubuntu(在 Vagrant 中)上使用 Tmux 1.8 它工作正常。
扩展 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
*请注意,${pwd##*/}
被转义为 ${pwd####*/}
,因为 #
在格式字符串中具有特殊含义。
**有关默认 tmux 配置示例,请参阅 here。
显示前 N 个组件
只显示基本名称会产生太多歧义,但完整路径会太混乱,所以我选择了:
the/last/path
而不是:
/a/very/long/the/last/path
或者只是:
path
.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]|'
技巧取自:Remove part of path on Unix
如果仍然不能解决歧义,我继续:
bind-key -r w choose-window -F '#{window_index} | #{pane_current_command} | #{host} | #{pane_current_path}'
在 Tmux 2.1、Ubuntu 16.04 上测试。
我正在为此使用 zsh 钩子
在~/.zshrc
precmd () {
if [ -n "$TMUX" ]; then
tmux set-window-option -q window-status-format "#[fg=cyan bg=cyan] | #[fg=white, bg=cyan] #I | ${PWD##/*/} #[fg=cyan, bg=cyan] | "
tmux set-window-option -q window-status-current-format "#[fg=cyan, bg=cyan] | #[fg=white, bg=cyan] #I | ${PWD##/*/} #[fg=cyan, bg=cyan] | "
fi
}
我在 ~/.tmux.conf 中使用以下内容来实现此目的(在 OSX、zsh、tmux-2.3 上工作):
set -g automatic-rename-format '#{pane_current_path}'
set -g status-interval 5
您可以将 status-interval 设置为 1 以使其更快地响应更改目录。
根据变更日志 (https://raw.githubusercontent.com/tmux/tmux/master/CHANGES) 这应该适用于 tmux 1.9 及更高版本。
在装有 tmux 2.3 的 CentOS 机器上使用 ssh window 名称不会改变,直到我在新面板中按下 return,不知道为什么会这样。
这并没有严格回答您的问题——它不会自动将现有的 tmux 会话重命名为当前工作目录。
相反,在创建新会话时,它会以当前工作目录命名该会话。
这是我所做的:
到
~/.aliases
添加
alias tm='tmux new -s `basename $PWD`'
打开一个新终端 window 并输入:
tm
现在这将创建一个新的 tmux 会话,该会话以当前工作目录命名。
注意:这依赖于 basename
,Windows 中不存在。
使用 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}'
man tmux
的 FORMATS 部分描述了其他修饰符,例如 #{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}")'
我确定你想使用这个:
set -g status-left '#{pane_current_path} '
要更改您在 window 列表中看到的内容,您可以在为 chose-window
函数定义键绑定时指定一种格式,如下所示:
bind-key '"' choose-window -F "#{session_name} | #{window_name} - #{b:pane_current_path} (#{pane_current_command})"
为了两全其美 - 当你在 shell 提示时,window 名称是路径,但当你在 运行 时,名称是可执行文件的名称,请尝试这个:
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}}"
将“bash”替换为您正在使用的 shell。